将double插入char *数组元素

时间:2019-02-24 04:29:50

标签: c++ exec

如何将双精度数据类型插入char*数组中?我已经尝试过使用std::to_string()c_str(),但是似乎都没有用。

本质上,我试图使用execvp()调用c ++程序,从读取man page开始,我认为它需要包含一个char*数组。

我试图调用该程序,然后将双精度a, b, and c传递给程序argv的{​​{1}}参数。

E.G。 execvp() 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; 不能真正以这种循环的方式工作,因为它代替了当前程序,但是我知道以后如何处理,只是试图使args正常工作

3 个答案:

答案 0 :(得分:2)

此代码有两个问题:

  • 当您尝试使用临时对象填充“ char指针数组” args时,这将无法反映您的需求。改用(见下文)const char* const args[],并用指向真实对象数据tempa,tempb和tempc的指针填充它。

  • execvp期望char* const[],而在C ++中,您总是会生成const char* const[],因此您需要强制转换execvp(const_cast<char* const*>(args));

您可以将代码更改为,然后它将起作用:

#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <string>
int main()
{
    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){
                std::string tempa = std::to_string(a); 
                std::string tempb = std::to_string(b); 
                std::string tempc = std::to_string(c);
                const char* const args[] = {tempa.c_str(), tempb.c_str(), tempc.c_str(), nullptr};
                std::cout << "Calling: " << a << " " << b << " " << c << std::endl;
                execvp(args[0], const_cast<char*const*>(args));
            }
        }
    }
    return 0;
}

(已测试)

答案 1 :(得分:0)

的使用
args[1] = temp.c_str();

是一个问题,因为LHS的类型为char*,而RHS的类型为char const*。您无法将char const*分配给char*

一个简单的解决方法是使用:

args[1] = strdup(temp.c_str());

strdup不是标准库函数,但是某些编译器本身支持它。如果您的编译器不支持它,那么自己实现它就不会太困难。在网上查找,您很可能会找到一个。

请确保您取消分配strdup返回的字符串。您必须查阅编译器的文档,以了解如何在strdup中分配内存。您需要使用适当的释放机制。

答案 2 :(得分:0)

您的第一种方式,即

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

是正确的,除了需要将声明char *args[5];更改为const char *args[5];。当然,如果要传递3个不同的命令行参数,则需要3个不同的temp变量(temp1, temp2, temp3)。