【问题标题】:c++ system commandc++系统命令
【发布时间】:2012-08-24 11:51:03
【问题描述】:

我已经编写了一个示例 c++ 程序...这里我使用系统命令调用带有参数的 python 程序...

system("python /home/rpms/a3/dsp/noise_rem_python.py /home/rpms/a3/dsp/files/p1f%d.txt",tid);

/home/rpms/a3/dsp/noise_rem_python.py is a program name

/home/rpms/a3/dsp/files/p1f%d.txt 是这个程序的一个参数。

但我收到错误:

"/usr/include/stdlib.h:在函数‘void* writefile(void*)’中: /usr/include/stdlib.h:712: 错误: 函数‘int system(const char*)’的参数太多 writefile.cpp:29: 错误:此时在文件中"

【问题讨论】:

  • C++ 并不神奇。如果空气稀薄,它不仅会为您格式化字符串。

标签: c++ system-calls


【解决方案1】:

你也可以这样做:

char command[200];    // 200 is just an example value that can hold the whole string
sprintf(command, "python /home/rpms/a3/dsp/noise_rem_python.py /home/rpms/a3/dsp/files/p1f%d.txt", tid);
system(command);

如果你想以相同的风格来做。

【讨论】:

    【解决方案2】:

    这样说:

    #include <string>
    
    system(("python /home/rpms/a3/dsp/noise_rem_python.py /home/rpms/a3/dsp/files/p1f" + std::to_string(tid) + ".txt").c_str());
    

    【讨论】:

    • 您在字符串表达式周围缺少一个开头 (
    • @JamesKanze:谢谢,已修复!我最初在第一部分周围有一个std::string,但实际上不需要。
    • std::to_string() 仅适用于 C++11。我们不知道OP是否可以使用它。
    • 如果不能选择 C++11,boost::lexical_cast&lt;std::string&gt;(tid) 是一个替代方案。
    【解决方案3】:

    查看你传递给函数的参数的结尾

    ... ,tid);

    如果你想格式化一个字符串?在将其用作参数之前执行此操作。

    【讨论】:

      猜你喜欢
      • 2011-09-24
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 2011-11-19
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多