1 popen介绍
我是在ubuntu上面进行man popen的,解释如下
这个函数通过创建一个管道通过fork一个进程,然后执行一个command,因为在管道中,所以数据流是单向的,然后type一般只能是读“r”或者写“w”,返回值在IO流里面,用了popen之后我们要记得用pclose函数。
2 使用
#include <stdio.h>
#define len 512
int main()
{
char buf[len] = {0};
FILE *fp = NULL;
if ((fp = popen("cat 1.txt", "r")) == NULL)
{
printf("popen fail\n");
return -1;
}
while (fgets(buf, 200, fp) != NULL)
{
printf("%s", buf);
}
pclose(fp);
if ((fp = popen("echo chenyu > 2.txt", "w")) == N