Running shell commands by C++
- #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++的更多相关文章
- Testing shell commands from Python
如何测试shell命令?最近,我遇到了一些情况,我想运行shell命令进行测试,Python称为万能胶水语言,一些自动化测试都可以完成,目前手头的工作都是用python完成的.但是无法从Python中 ...
- Run Shell Commands in Python
subprocess.call This is the recommended way to run shell commands in Python compared with old-fashio ...
- Frequently Used Shell Commands
[Common Use Shell Commands] 1.ps aux:查看当前所有进程 ,以用户名为主键.可以查看到 USER.PID.COMMAND(binary所有位置) 2.netstat ...
- 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 ...
- 在R中运行Shell命令脚本(Call shell commands from R)
aaa.R Args <- commandArgs()cat("Args[1]=",Args[1],"\n")cat("Args[2]=&quo ...
- mysql 使用 informatin_schema tables 创建 shell commands
SELECT CONCAT("mysqldump -uroot -p ", TABLE_SCHEMA, " ", TABLE_NAME, " > ...
- Hadoop FS shell commands
命令格式:hadoop fs -command -option args appendToFileUsage: hadoop fs -appendToFile <localsrc> ... ...
- echo shell commands as they are executed
http://stackoverflow.com/questions/2853803/in-a-shell-script-echo-shell-commands-as-they-are-execute ...
- Shelled-out Commands In Golang
http://nathanleclaire.com/blog/2014/12/29/shelled-out-commands-in-golang/ Shelled-out Commands In Go ...
随机推荐
- IDM使用教程:利用IDM下载百度网盘文件
IDM是什么 其实我使用IDM下载器只是为了方便网页版百度网盘直接下载大于40M文件而已,大家知道文件过大必须打开客户端才能下载,这点对于我的破电脑感觉很烦躁,每次要等待它慢悠悠打开,然后动用我的超级 ...
- 六问六答理解ForkJoin原理
摘要:ForkJoin线程池是将任务分割为子任务,有可能子任务还是很大,还需要进一步拆解,最终得到足够小的任务. 本文分享自华为云社区<ForkJoin线程池的学习和思考>,作者:brea ...
- 『学了就忘』Linux基础命令 — 35、网络中与其他机器通信的命令
目录 1.write命令 2.wall命令 3.mail 命令 使用1:发送邮件 使用2:查看已经接收的邮件 使用3:发送文件内容 1.write命令 (1)write命令的基本信息 命令名称:wri ...
- 使用grep命令,玩转代码审计寻找Sink
好久没分享东西了,今天分享个实用代码审计技巧 使用grep,玩转代码审计,适用于linux/mac,windows需要另行安装grep: 使用场景如下:快速寻找项目中所有的Sink,快速寻找符合适配条 ...
- Java将增加虚拟线程,挑战Go协程
我们知道 Go 语言最大亮点之一就是原生支持并发,这得益于 Go 语言的协程机制.一个 go 语句就可以发起一个协程 (goroutin).协程本质上是一种用户态线程,它不需要操作系统来进行调度,而是 ...
- 猿猿有责,维持整洁的 Git 提交记录,三个锦囊送给你
背景 大家都有学习如何规范简洁的编写代码,但却很少学习如何规范简洁的提交代码.现在大家基本上都用 Git 作为源码管理的工具,Git 提供了极大的灵活性,我们按照各种 workflow 来提交/合并 ...
- [atARC109F]1D Kingdom Builder
考虑最终有石子的位置的状态,判断一种状态是否可行 反过来,依次删除石子,删除条件是:当删除的石子是该段最后一个(即其两边都没有石子了),要求除其以外,每个连续段旁边的两个点都与其颜色不同 构造一种删除 ...
- Spring Cloud Gateway限流实战
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- idea文件多行显示
将红色部分取消勾选
- pm2 重启策略(restart strategies)
使用 PM2 启动应用程序 时,应用程序会在自动退出.事件循环为空 (node.js) 或应用程序崩溃时自动重新启动. 但您也可以配置额外的重启策略,例如: 使用定时任务重新启动应用程序 文件更改后重 ...