linux popen函数简单实例
来源:互联网 发布:ws小世界网络模型 编辑:程序博客网 时间:2024/06/06 11:40
Linux 中的popen机制可以在程序中执行一个shell命令,有两种操作模式,分别为读和写。在读模式中,程序中可以读取到命令的输出,其中有一个应用就是获取网络接口的参数。在写模式中,最常用的是创建一个新的文件或开启其他服务等。
<span style="font-size:18px;">#include <stdlib.h>#include <stdio.h>#define BUF_SIZE 1024char buf[BUF_SIZE];int main(void){ FILE * p_file = NULL; p_file = popen("ifconfig eth0", "r"); if (!p_file) { fprintf(stderr, "Erro to popen"); } while (fgets(buf, BUF_SIZE, p_file) != NULL) { fprintf(stdout, "%s", buf); } pclose(p_file); p_file = popen("touch test.tmp", "w"); if (!p_file) { fprintf(stderr, "Erro to popen"); } while (fgets(buf, BUF_SIZE, p_file) != NULL) { fprintf(stdout, "%s", buf); } pclose(p_file); p_file = popen("touch test.tmp", "w"); if (!p_file) { fprintf(stderr, "Erro to popen"); } pclose(p_file); return 0;}</span>
运行后结果:
eth0 Link encap:Ethernet HWaddr 00:0c:29:db:ac:05
inet6 addr: fe80::20c:29ff:fedb:ac05/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:306065 errors:1 dropped:0 overruns:0 frame:0
TX packets:291821 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:130350798 (130.3 MB) TX bytes:119043669 (119.0 MB)
Interrupt:19 Base address:0x2000
CMD: ls
test.tmp
0 0
- linux popen函数简单实例
- linux popen函数
- Linux popen()函数使用
- linux popen函数
- linux popen函数
- linux popen函数
- linux 之 popen函数
- linux popen函数
- Linux popen 函数
- linux popen函数学习
- linux popen函数
- linux popen函数
- linux popen函数
- Linux 的popen函数
- Linux中的popen函数
- linux popen函数
- linux下的popen()函数
- Linux popen和pclose函数
- Yii - data exists update else insert
- Spring MVC程序中得到静态资源文件css,js,图片文件的路径
- OC--小知识总结
- IOException while loading persisted sessions: java.io.EOFException
- 开发Rpg maker第一弹
- linux popen函数简单实例
- hdu 2047(递推)
- uva 580
- 【读书笔记】Java 线程
- 最短路坑题——贝西回家 uscow2.2.4
- Linux下常用命令总结
- WinDDK+VS2010驱动环境配置
- MySQL - data exists update else insert
- unity3D学习 1