作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个 C++ 代码,我从用户那里获取进程名称,然后使用命令 pgrep process_name
获取进程 ID,然后使用命令 kill process_id
终止它,问题是我无法将命令 pgrep
的输出保存在变量中以再次使用它,这就是我到目前为止所达到的我正在使用 ubuntu 的终端
p.s(stringcat
是一个我疯狂地连接命令中使用的单词的函数)
cout<<"enter the name of the process you wanna stop : ";
cin>>in;
string PID;
command=stringcat("pgrep ",in,"");
/*
const char*temp = command.c_str();
PID=system(temp);
*/
command=stringcat("kill",PID,"");
const char*temp2 = command.c_str();
system(temp2);
最佳答案
popen
是最简单的选择,或者您可以使用 fork
并将子进程的 stdout 替换为您想要的。
关于c++ - 如何将终端的输出保存到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36793718/
我是一名优秀的程序员,十分优秀!