标签:
/************************************************************************* > File Name: popen4.c > Description: popen4.c程序演示popen函数的行为,它对所有popen示例程序的源文件的总行数进行统计 > Author: Liubingbing > Created Time: 2015年07月09日 星期四 20时39分42秒 > Other: popen4.c ************************************************************************/ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> int main() { FILE *read_fp; char buffer[BUFSIZ + 1]; int chars_read; memset(buffer, '\0', sizeof(buffer)); read_fp = popen("cat popen*.c | wc -l", "r"); if (read_fp != NULL) { chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp); printf("chars_read = %d\n", chars_read); while (chars_read > 0) { buffer[chars_read - 1] = '\0'; printf("Reading:-\n %s\n", buffer); chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp); } pclose(read_fp); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE); }这个程序显示,shell在启动后将popen*.c扩展为一个文件列表,列表中的文件名都以popen开头,以.c结尾,shell还处理了管道符|,并将cat命令的输出传递给wc命令.在一个popen调用中启动了shell,cat程序和wc程序,并进行了一次输出重定向.而调用这些命令的程序只看到最终的输出结果.
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/yiranant/article/details/46821977