【问题标题】:Inserting double into char* array element将双精度插入 char* 数组元素
【发布时间】:2019-02-24 04:29:49
【问题描述】:

如何将双精度数据类型插入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 的传递起作用。

【问题讨论】:

  • 我删除了 C 标签。当您需要 C++ 方面的帮助时,请不要标记 C。
  • 好的,这是一个 C 函数,所以我认为这很重要,但请注意。
  • 你显然是在用 C++ 编译器编译它,这对所有的 C 代码都是不可能的。

标签: c++ exec


【解决方案1】:

这段代码有两个问题:

  • 当你尝试时,你不能用临时对象填充你的“char 指针数组”args,这不会反映你的需要。改用(见下文)const char* const args[] 并用指向真实对象数据 tempa、tempb、tempc 的指针填充它。

  • execvp 期望 char* const[] 而在 C++ 中您总是会产生 const char* const[],因此您需要转换类似 execvp(const_cast&lt;char* const*&gt;(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;
}

(已测试)

【讨论】:

    【解决方案2】:

    使用

    args[1] = temp.c_str();
    

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

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

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

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

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

    【讨论】:

      【解决方案3】:

      你的第一种方式,即

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多