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 ...
随机推荐
- 【接口】HttpClient 处理get和post请求(二)(2019-07-14 18:41)
一.环境准备 1.导入httpClient依赖包 <dependency> <groupId>org.apache.httpcomponents</groupId> ...
- JDK 工具 HSDB 查看动态生成类
前置工作 1. 复制 JDK 安装目录\jre\bin\sawindbg.dll 到 JDK 安装目录同级的 jre\bin 目录下,否则会报错找不到 sawindbg.dll 文件. 比如我的 sa ...
- PHP 数组函数分类整理
1.处理数组键名相关的函数: array_change_key_case - 返回字符串键名全为小写或大写的数组. array_key_exists - 检查给定的键名或索引是否存在于数组中 arra ...
- tomcat隐藏版本号
默认报错页面信息会暴露出版本号 进入tomcat的lib目录找到catalina.jar文件 unzip catalina.jar之后会多出两个文件夹 进入org/apache/catalina/ut ...
- [gym101981F]Frank
在本题中,每一步是独立的,因此即可以看作从$s$移动到$t$的期望步数(对于每一对$s$和$t$都求出答案) 令$f_{i,j}$表示当$s=i$且$t=j$时的答案,则有$f_{i,j}=\begi ...
- [atARC101F]Robots and Exits
每一个点一定匹配其左边/右边的第一个出口(在最左/右边的出口左/右边的点直接删除即可),否则记到左右出口的距离分别为$x_{i}$和$y_{i}$ 令$p_{i}$表示$i$匹配的出口(左0右1),结 ...
- [noi1773]function
以统计x坐标的数量为例:x为下标建一棵线段树,然后对每一个区间按照y坐标建一棵可持久化线段树(每一个x只保留最大的一个y),询问时,二分找到这个区间内最大的y以前的点并统计,复杂度为$o(nlog^{ ...
- CG Kit探索移动端高性能渲染
内容来源:华为开发者大会2021 HMS Core 6 Graphics技术论坛,主题演讲<CG Kit探索移动端高性能渲染> 演讲嘉宾:华为海思麒麟GPU团队工程师 大家好,我来自华为海 ...
- javascript-初级-day04js数据类型
day01-js数据类型 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type& ...
- Insights直播回顾,对话专家,HMS Core 6创新能力解读
HMS Core Insights第八期直播–对话专家,HMS Core 6创新能力解读,已于11月25日圆满结束,本期直播与小伙伴们一同了解了HMS Core 6在图形.媒体以及连接与通信领域推出的 ...