1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6. string cmdinput;
  7. string GetStdoutFromCommand(string cmd) {
  8.  
  9. string data;
  10. FILE * stream;
  11. const int max_buffer = 256;
  12. char buffer[max_buffer];
  13. cmd.append(" 2>&1");
  14.  
  15. stream = popen(cmd.c_str(), "r");
  16. if (stream) {
  17. while (!feof(stream))
  18. if (fgets(buffer, max_buffer, stream) != NULL) data.append(buffer);
  19. pclose(stream);
  20. }
  21. return data;
  22. }
  23.  
  24. int main (){
  25. cout << "Enter your command:" << endl;
  26. string cmdinput;
  27. getline (cin, cmdinput);
  28. cout << "Your command was '" << cmdinput << "'" << endl;
  29. string com = GetStdoutFromCommand(cmdinput);
  30. cout << "Command: " << com << endl;
  31.  
  32. return 0;
  33. }

in this way you can easy get the output string of your input command in C++

refer to http://www.cplusplus.com/forum/unices/144187/

Running shell commands by C++的更多相关文章

  1. Testing shell commands from Python

    如何测试shell命令?最近,我遇到了一些情况,我想运行shell命令进行测试,Python称为万能胶水语言,一些自动化测试都可以完成,目前手头的工作都是用python完成的.但是无法从Python中 ...

  2. Run Shell Commands in Python

    subprocess.call This is the recommended way to run shell commands in Python compared with old-fashio ...

  3. Frequently Used Shell Commands

    [Common Use Shell Commands] 1.ps aux:查看当前所有进程 ,以用户名为主键.可以查看到 USER.PID.COMMAND(binary所有位置) 2.netstat ...

  4. Running multiple commands in one line in shell

      You are using | (pipe) to direct the output of a command into another command. What you are lookin ...

  5. 在R中运行Shell命令脚本(Call shell commands from R)

    aaa.R Args <- commandArgs()cat("Args[1]=",Args[1],"\n")cat("Args[2]=&quo ...

  6. mysql 使用 informatin_schema tables 创建 shell commands

    SELECT CONCAT("mysqldump -uroot -p ", TABLE_SCHEMA, " ", TABLE_NAME, " > ...

  7. Hadoop FS shell commands

    命令格式:hadoop fs -command -option args appendToFileUsage: hadoop fs -appendToFile <localsrc> ... ...

  8. echo shell commands as they are executed

    http://stackoverflow.com/questions/2853803/in-a-shell-script-echo-shell-commands-as-they-are-execute ...

  9. Shelled-out Commands In Golang

    http://nathanleclaire.com/blog/2014/12/29/shelled-out-commands-in-golang/ Shelled-out Commands In Go ...

随机推荐

  1. IDM使用教程:利用IDM下载百度网盘文件

    IDM是什么 其实我使用IDM下载器只是为了方便网页版百度网盘直接下载大于40M文件而已,大家知道文件过大必须打开客户端才能下载,这点对于我的破电脑感觉很烦躁,每次要等待它慢悠悠打开,然后动用我的超级 ...

  2. 六问六答理解ForkJoin原理

    摘要:ForkJoin线程池是将任务分割为子任务,有可能子任务还是很大,还需要进一步拆解,最终得到足够小的任务. 本文分享自华为云社区<ForkJoin线程池的学习和思考>,作者:brea ...

  3. 『学了就忘』Linux基础命令 — 35、网络中与其他机器通信的命令

    目录 1.write命令 2.wall命令 3.mail 命令 使用1:发送邮件 使用2:查看已经接收的邮件 使用3:发送文件内容 1.write命令 (1)write命令的基本信息 命令名称:wri ...

  4. 使用grep命令,玩转代码审计寻找Sink

    好久没分享东西了,今天分享个实用代码审计技巧 使用grep,玩转代码审计,适用于linux/mac,windows需要另行安装grep: 使用场景如下:快速寻找项目中所有的Sink,快速寻找符合适配条 ...

  5. Java将增加虚拟线程,挑战Go协程

    我们知道 Go 语言最大亮点之一就是原生支持并发,这得益于 Go 语言的协程机制.一个 go 语句就可以发起一个协程 (goroutin).协程本质上是一种用户态线程,它不需要操作系统来进行调度,而是 ...

  6. 猿猿有责,维持整洁的 Git 提交记录,三个锦囊送给你

    背景 大家都有学习如何规范简洁的编写代码,但却很少学习如何规范简洁的提交代码.现在大家基本上都用 Git 作为源码管理的工具,Git 提供了极大的灵活性,我们按照各种 workflow 来提交/合并 ...

  7. [atARC109F]1D Kingdom Builder

    考虑最终有石子的位置的状态,判断一种状态是否可行 反过来,依次删除石子,删除条件是:当删除的石子是该段最后一个(即其两边都没有石子了),要求除其以外,每个连续段旁边的两个点都与其颜色不同 构造一种删除 ...

  8. Spring Cloud Gateway限流实战

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  9. idea文件多行显示

    将红色部分取消勾选

  10. pm2 重启策略(restart strategies)

    使用 PM2 启动应用程序 时,应用程序会在自动退出.事件循环为空 (node.js) 或应用程序崩溃时自动重新启动. 但您也可以配置额外的重启策略,例如: 使用定时任务重新启动应用程序 文件更改后重 ...