#include <iostream>
#include <stdio.h>
#include <stdlib.h> using namespace std;
string cmdinput;
string GetStdoutFromCommand(string cmd) { string data;
FILE * stream;
const int max_buffer = 256;
char buffer[max_buffer];
cmd.append(" 2>&1"); stream = popen(cmd.c_str(), "r");
if (stream) {
while (!feof(stream))
if (fgets(buffer, max_buffer, stream) != NULL) data.append(buffer);
pclose(stream);
}
return data;
} int main (){
cout << "Enter your command:" << endl;
string cmdinput;
getline (cin, cmdinput);
cout << "Your command was '" << cmdinput << "'" << endl;
string com = GetStdoutFromCommand(cmdinput);
cout << "Command: " << com << endl; return 0;
}

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. SpringCloud升级之路2020.0.x版-29.Spring Cloud OpenFeign 的解析(1)

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在使用云原生的很多微服务中,比较小规模的可能直接依靠云服务中的负载均衡器进行内部域名与服务 ...

  2. dart系列之:dart语言中的函数

    目录 简介 函数的参数 main函数 匿名函数 闭包 函数的返回值 总结 简介 函数是所有编程语言都有的内容,不管是面向对象还是面向过程,函数都是非常重要的一部分.dart中的函数和java中的函数有 ...

  3. node 读取文件内容并响应

    node 读取文件内容并响应 const http = require('http'); const fs = require('fs') //创建 Server const server = htt ...

  4. mybatis bind 标签

    bind 标签可以使用 OGNL 表达式创建一个变量井将其绑定到上下文中.在前面的例子中, UserMapper.xml 有一个 selectByUser 方法,这个方法用到了 like 查询条件,部 ...

  5. Duboo整合SpringBoot超级详细例子(附源码)

    dubbo3.0整合SpringBoot例子 dubbo新版本(3.0以上)在相对于 dubbo 旧版本(2.5.2.6.2.7),有很多的不相同的地方. 官方文档也说了新版本的特性: https:/ ...

  6. 学不懂Netty?看不懂源码?不存在的,这篇文章手把手带你阅读Netty源码!

    阅读这篇文章之前,建议先阅读和这篇文章关联的内容. 1. 详细剖析分布式微服务架构下网络通信的底层实现原理(图解) 2. (年薪60W的技巧)工作了5年,你真的理解Netty以及为什么要用吗?(深度干 ...

  7. MySQl安装图形界面

    对于mysql的图形界面有很多个:1.MySQL GUI Tools MySQL GUI Tools是一个可视化界面的MySQL数据库管理控制台,提供了四个非常好用的图形化应用程序,方便数据库管理和数 ...

  8. [atARC105F]Lights Out on Connected Graph

    记$G[S]$表示图$G$在点集$S$上的导出子图,即$G[S]=(S,{(x,y)|x,y\in S且(x,y)\in E})$ 定义$g(S)$为所有$E'$(满足$E'\subseteq G[S ...

  9. OAuth 2.1 带来了哪些变化

    OAuth 2.1 是 OAuth 2.0 的下一个版本, OAuth 2.1 根据最佳安全实践(BCP), 目前是第18个版本,对 OAuth 2.0 协议进行整合和精简, 移除不安全的授权流程, ...

  10. vue 事件监听和es6模板语法

    es6模板语法的反引号是通过左上角的飘字符弄出来了,学废了吗?