4
當我在我的Mac(OS/X 10.6.4)運行下面的程序,我得到下面的輸出:爲什麼popen()只能工作一次?
$ ./a.out
Read 66 lines of output from /bin/ps
Read 0 lines of output from /bin/ps
Read 0 lines of output from /bin/ps
Read 0 lines of output from /bin/ps
Read 0 lines of output from /bin/ps
爲什麼是它popen方法()只讀取數據時,我第一次稱呼它,並隨後的通行證返回沒有輸出?
#include <stdio.h>
int main(int argc, char ** argv)
{
int i;
for (i=0; i<5; i++)
{
FILE * psAux = popen("/bin/ps ax", "r");
if (psAux)
{
char buf[1024];
int c = 0;
while(fgets(buf, sizeof(buf), psAux)) c++;
printf("Read %i lines of output from /bin/ps\n", c);
fclose(psAux);
}
else printf("Error, popen() failed!\n");
sleep(1);
}
}