使用popen的好处只是读取命令产生的输出,或者系统中有更多的好处或优点。popen vs C中的系统功能
考虑下面两个方案:
计划1:
#include <stdio.h>
int main()
{
FILE *more,*who;
if (!(more = popen("more", "w")))
{
printf("Command `more` not found!");
return -1;
}
if (!(who = popen("who", "r")))
{
printf("Command `who` not found!");
return -1;
}
while (!feof(who))
{
char buffer[100];
if (fgets(buffer, 100, who) != NULL)
{
fputs(buffer, more);
}
}
fclose(more);
fclose(who);
return 0;
}
方案2:
#include <unistd.h>
int main()
{
system("who|more");
return 0;
}
我为什么要使用PROGRAM1如果我能做到同样的事情在一个线如在Program2中所做的那样。
如果你做了一点研究,你会遇到[这个堆栈溢出线程](http://stackoverflow.com/questions/8538324/what-is-the-difference-between-popen-and-system-在-C)。 – 2013-05-07 06:46:33
请注意,您应该在使用popen()打开的文件描述符上使用pclose()。 – 2013-11-16 12:11:41