Ubuntu使用C调用Shell
来源:互联网 发布:爱淘网 淘宝 编辑:程序博客网 时间:2024/06/01 11:55
#include <stdlib.h>#include <stdio.h>#include <iostream>#include <string>using namespace std;/* 执行系统命令,返回命令执行结果字符串*/string get_output_of_cmd(const string &cmd){ int32_t count(2048); char s[2048]; string ret; FILE* stream = popen(cmd.c_str(), "r"); if (stream != NULL) { // 每次从stream中读取指定大小的内容 while (fgets(s, count, stream)) ret += s; pclose(stream); } return ret;}/* 执行系统命令,根据命令退出代码返回布尔值*/bool get_exit_status_of_cmd(const string &cmd){ return (system(cmd.c_str()) == 0);}int main (int argc, char** argv){ //get_output_of_cmd("ls -al /home/caolei"); //get_exit_status_of_cmd("ls -al /home/caolei"); //get_exit_status_of_cmd("/data/test.sh"); int a = system("ls"); cout << a << endl; string b = get_output_of_cmd("ls"); cout << b << endl; printf("hello world.\n"); return 0;}
使用g++编译,不要使用gcc:
g++ cpp_to_shell.cpp -o cpp_to_shell.out
注意:popen函数是重点。
阅读全文
0 0
- Ubuntu使用C调用Shell
- linux c使用system调用shell脚本
- 在C程序中使用popen调用shell脚本程序
- jni使用c语言调用android shell命令方法
- C语言 调用 shell 命令
- C语言调用SHELL脚本
- C程序调用shell脚本
- C函数调用shell脚本
- C中调用shell 脚本
- C函数调用shell脚
- Objective-C调用shell脚本
- C语言调用shell命令
- c语言调用shell命令
- ubuntu下php调用执行shell脚本
- 使用dbms_scheduler调用shell脚本
- 使用shell命令调用matlab
- ubuntu shell 使用命令大全
- ubuntu 默认使用dash shell
- PHP新特性 闭包
- jmap,jhat分析内存
- 数据结构与算法专题之树——二叉树的遍历及应用
- 学生信息查询系统学习总结
- Maze (opentrains)
- Ubuntu使用C调用Shell
- Conscription (Kruskal+优先队列)
- 2.修改/etc/hosts文件 /安装SSH、配置SSH无密码登陆(全程切换到hadoop用户下操作)
- docker部署laravel报错界面只有whoops没有调试信息
- (25) 异常 (下) / 计算机程序的思维逻辑
- 小游戏之飞机躲子弹
- 拍黄片
- 基于以太坊的区块链浏览器(java web开发)
- 隐马尔科夫链