Как вставить двойной тип данных в массив char*
?Я пытался использовать std::to_string()
, а также c_str()
, однако ни один из них не помог.
По сути, я пытаюсь вызвать программу на c ++, используя execvp()
, которая при чтении справочной страницы Я думаю, что она должна принимать массив char*
.
Я пытаюсь вызвать программу, а затем передать двойные значения a, b, and c
в качестве параметров argv
для программы, вызываемой execvp()
.
EG, когда a = 0.1, b =0.1, c = 0.1
execvp(args[0], args)
должен сделать то же самое, что итерминальный вызов: ./RL 0.1 0.1 0.1
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <string>
int main()
{
char *args[5];
args[0] = "RL";
std::string temp;
for(double a = 0.1; a < 1; a += 0.1){
for(double b = 0.1; b < 1; b += 0.1){
for(double c = 0.1; c < 1; c+= 0.1){
temp = std::to_string(a); //attempted to convert the double to a string
args[1] = temp.c_str(); //attempted to convert the string to c string
temp = std::to_string(b);
args[2] = temp; //attempted to just insert the string
args[3] = c; //attempted to insert as double
std::cout << "Calling: " << a << " " << b << " " << c << std::endl;
execvp(args[0], args);
}
}
}
return 0;
}
Я получаю такие ошибки:
RLdriver.cpp:14:32: error: assigning to 'char *' from incompatible type 'const
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >::value_type *' (aka 'const char *')
args[1] = temp.c_str();
~~~~~^~~~~~~
RLdriver.cpp:16:27: error: assigning to 'char *' from incompatible type
'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char>
>')
args[2] = temp;
^~~~
RLdriver.cpp:17:27: error: assigning to 'char *' from incompatible type 'double'
args[3] = c;
РЕДАКТИРОВАТЬ: я знаю, exec()
не будет работать в этом смысле цикла, как этозаменяет текущую программу, но я знаю, как с этим справиться позже, просто пытаясь заставить прохождение аргументов работать.