String cmd = "/alcatel/omc3/osm/script/proc_upd.pl -s stop -p MFSUSMCTRL -u" + userName;
system(cmd);
I want to print the output of system()
function. How can I do that ?
String cmd = "/alcatel/omc3/osm/script/proc_upd.pl -s stop -p MFSUSMCTRL -u" + userName;
system(cmd);
I want to print the output of system()
function. How can I do that ?
You can use the function popen
. It will allow you to get the result of a command. You must add #include <stdio.h>
to your code to use this. The basic syntax is FILE * file_name = popen("command", "r")
. Your code might look something like:
#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;
}