将整数值传递给系统调用命令

Passing integer value to system evoking command

本文关键字:系统调用 命令 整数 值传      更新时间:2023-10-16

有人可以告诉我如何将整数值传递给命令语句吗?

int lf,ff;
string cmd,filename;
cmd = "catdcd -o -otype frame.xyz -first "+lf+" -last "+ff+" "+filename+"";
system((cmd).c_str());

错误:类型不匹配"const __gnu_cxx::__normal_iterator<_Iterator、_Container>"和"int">

我刚刚尝试使用...

cmd = "catdcd -o -otype frame.xyz -first "lf" -last "ff" "+filename+"";

但最终又出现了另一个错误。

错误:无法将参数"1"的"std::string {aka std::basic_string}"转换为"char*"到"int sprintf(char*, const char*, ...(">

请帮忙。我必须增加lf的值,并为我的程序使用循环ff

我对C++相当陌生,所以我不知道这样做的最佳方法是什么,但这应该有效。

#include<sstream>
stringstream ss;
ss << "catdcd -o -otype frame.xyz -first " << lf << " -last " << ff << " " << filename;
string cmd = ss.str();

如果您使用的是C++ 11,则可以使用to_string()函数,因此类似于

int lf, ff;
string cmd, filename;
cmd = "catdcd -o -otype frame.xyz -first " + to_string(lf) + " -last " + to_string(ff) + " " + filename + "";
system((cmd).c_str());

应该做这项工作。