- 论坛徽章:
- 0
|
#include
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
说明:
popen使用FIFO管道执行外部程序。
popen() 函数 用 创建管道的 方式启动一个进程, 并调用 shell. 因为管道是被定义成单向的, 所以 type 参数只能定义成只读或者 只写, 不能是两者同时, 结果流也相应的 是只读或者只写.
参数:
popen 通过type是r还是w确定command的输入/输出方向,r和w是相对command的管道而言的。r表示command从管道中读入,w表示 command通过管道输出到它的stdout,popen返回FIFO管道的文件流指针。pclose则用于使用结束后关闭这个指针。
示例:
#include
#include
#include
#include
#include
int main( void )
{
FILE *stream;
char buf[1024];
memset( buf, '\0', sizeof(buf) ); /*初始化buf,以免后面写如乱码到文件中*/
if((stream = popen("./mnt/env_lcd","r")) == NULL ); /*将“ls -l”命令的输出 通过
管道读取(“r”参数)到FILE* stream*/
{
printf("popen() error!\n");
exit(1);
}
fread( buf, sizeof(char), sizeof(buf), stream);
/*将刚刚FILE* stream的数据流读取到buf中*/
pclose( stream );
return 0;
}
[/url]
[url=http://www.91linux.com/html/article/program/20071017/7637.html]
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/77869/showart_1899375.html |
|