String cmd = "/alcatel/omc3/osm/script/proc_upd.pl -s stop -p MFSUSMCTRL -u" + userName;
system(cmd);
我想打印system()
函数的输出。我怎么能这样做?
答案 0 :(得分:1)
您可以使用popen
功能。它将允许您获取命令的结果。您必须在代码中添加#include <stdio.h>
才能使用此代码。基本语法是FILE * file_name = popen("command", "r")
。您的代码可能类似于:
#include <iostream>
#include <stdio.h>
using namespace std;
char buf[1000];
string userName;
int main() {
cout << "What is your username?\nUsername:";
//input userName
cin >> userName;
//declare string strCMD to be a command with the addition of userName
string strCMD = "/alcatel/omc3/osm/script/proc_upd.pl -s stop -p MFSUSMCTRL -u" + userName;
//convert strCMD to const char * cmd
const char * cmd = strCMD.c_str();
//execute the command cmd and store the output in a file named output
FILE * output = popen(cmd, "r");
while (fgets (buf, 1000, output)) {
fprintf (stdout, "%s", buf);
}
pclose(output);
return 0;
}