Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C++;?_C++ - Fatal编程技术网

C++ 如何在C++;?

C++ 如何在C++;?,c++,C++,我想打印system()函数的输出。我该怎么做呢?您可以使用函数popen。它将允许您获得命令的结果。您必须在代码中添加#include,才能使用此功能。基本语法是FILE*FILE\u name=popen(“命令”,“r”)。您的代码可能类似于: String cmd = "/alcatel/omc3/osm/script/proc_upd.pl -s stop -p MFSUSMCTRL -u" + userName; system(cmd); #包括 #包括 使用名称空间

我想打印
system()
函数的输出。我该怎么做呢?

您可以使用函数
popen
。它将允许您获得命令的结果。您必须在代码中添加
#include
,才能使用此功能。基本语法是
FILE*FILE\u name=popen(“命令”,“r”)
。您的代码可能类似于:

String cmd = "/alcatel/omc3/osm/script/proc_upd.pl -s stop -p MFSUSMCTRL -u" + userName;      
system(cmd); 
#包括
#包括
使用名称空间std;
char-buf[1000];
字符串用户名;
int main(){
cout>用户名;
//将字符串strCMD声明为添加用户名的命令
字符串strCMD=“/alcatel/omc3/osm/script/proc_upd.pl-s stop-p MFSUSMCTRL-u”+用户名;
//将strCMD转换为const char*cmd
const char*cmd=strCMD.c_str();
//执行命令cmd并将输出存储在名为output的文件中
文件*output=popen(cmd,“r”);
而(fgets(buf,1000,输出)){
fprintf(标准输出,“%s”,buf);
}
pclose(输出);
返回0;
}

系统功能仅返回您运行的“程序”的退出代码,而不是将实际输出返回到其标准输出。请查看
popen
函数。@JoachimPileborg这是正确的答案。也许你可以写下来?
#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;
}