如何用C++打印system()输出
How to print system() output in C++?
String cmd = "/alcatel/omc3/osm/script/proc_upd.pl -s stop -p MFSUSMCTRL -u" + userName;
system(cmd);
我想打印system()
函数的输出。我该怎么做?
您可以使用函数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;
}
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 将 Linux system() 调用命令的输出重定向到仅可变的
- system()在ncurses中输出
- 如何获取 system() 命令 qt c++ 的输出
- 子输出出现在主进程使用 system() 调用它之前
- 如何从system()获得shell输出
- 函数 system() 中的输出重定向似乎不起作用
- 捕获c++system()命令的缓冲区输出
- 如何将system()函数的输出重定向到变量
- 通过 Ubuntu 上的 C++ system() 将输出与 SIPp 远程命令混淆
- 获取php system()函数的完整输出
- 在c++ /CLI中,是否有比system()更好的方法来重定向标准输出?
- 如何用C++打印system()输出