将system()的输出转换为变量

时间:2012-01-11 08:44:01

标签: c++ c linux service system

  

可能重复:
  Best way to capture stdout from a system() command so it can be passed to another function

在linux中获取服务的当前状态我编写了这段代码::

   char cmd[100];
   sprintf(cmd,"service %s status",argv[1]);
   system(cmd);

运行正常,它在控制台上显示输出:mysql正在运行或mysql已停止

但是我需要在字符串变量中输出这个控制台。如何在字符串变量中获取“mysql正在运行”,以便稍后可以使用此字符串变量。   thankx。

1 个答案:

答案 0 :(得分:1)

如果您想捕获输出,请使用popen()而不是system()

相关问题