开发者

How do I run another program from a C++ program

I am trying to call on another program to perform a function (I have no idea what, only that it is not written in C++, but in shell) on a file within my C++ program. I do not know how to actually perform the function within my program. I do know that I write something like this

system(PROGRAM HERE);

The problem is that I do not know exactly how I am supposed to type the program out. I believe that if the function were to be called dostuff, I would type out

system("dostuff");

... I think. But what if there are arguments attached to that function that I would give as variables within my C++ program? That is what I would really need help with. In the terminal, I would type in "dostuff -1 arg". So in C++ would I type开发者_Python百科 out

int arg = 5;

system("dostuff" arg); 


You could format the string first. With <sstream> included:

int arg = 5;
std::stringstream ss;
ss << "dostuff " << arg;
system(ss.str().c_str());

Alternatively, you could use the concatenation feature of std::string. If you prefer the C-style formatters, you could use snprintf to similar effect.


Simple answer is

system("dostuff -1 5");

Essentially you need to construct the terminal argument and pass that to the system call.

Ex:

int arg = 5;
std::stringstream command;

command << "dostuff -1 " << arg;

system(command.str());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜