将 double 插入 char* 数组元素

Inserting double into char* array element

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

本质上,我正在尝试使用 execvp() 调用一个 c++ 程序,从阅读 man page 我认为它需要接受一个 char* 数组。

我正在尝试调用该程序,然后将双打 a, b, and c 作为 argv 参数传递给正在调用的程序 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;

编辑:我知道 exec() 在这种循环意义上不会真正起作用,因为它取代了当前程序,但我知道稍后如何处理,只是试图让args 工作。

使用

args[1] = temp.c_str();

是一个问题,因为左轴是 char* 类型,而右轴是 char const* 类型。您不能将 char const* 分配给 char*

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

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

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

确保释放 strdup 返回的字符串。您必须查阅编译器的文档才能弄清楚 strdup 中的内存分配方式。您需要使用适当的释放机制。

你的第一种方法,即

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)。

这段代码有两个问题:

  • 您不能在尝试时用临时对象填充您的 "array of char pointers" 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;
}

(已测试)