I am trying to write a program which uses popen but I have a problem. Iwant to detect if the program I call with popen has ended. For example: #include <stdio.h>#include <sys/types.h>#include <unistd.h> int main(){FILE *pipe = NULL;int i=0; while(1) {if (i == 10) {pipe = popen("ls", "w");} if(pipe != NULL && feof(pipe)) {printf("Program has ended\n");} i++;usleep(1000);}} This doesn''t work. Does anyone know how to test if the program has ended ? Thanks,Bastiaan 推荐答案 ba ****** *****@gmail.com 写道: #我正在尝试编写一个使用popen的程序,但我遇到了问题。我想要检测我用popen调用的程序是否已经结束。例如: 你对popen / pclose没有那么多的控制权。获得EOF时,可用的标准输出已经耗尽了;当pclose回来时,孩子已经离开。如果你需要更好的级别控制,你必须自己执行fork(),pipe(),exec *()和 等待*()调用。 br /> - SM瑞恩 http://www.rawbw.com/~wyrmwif/ 谁领导这些暴民? ba***********@gmail.com wrote:# I am trying to write a program which uses popen but I have a problem. I# want to detect if the program I call with popen has ended. For example: You don''t have that much control with popen/pclose. The available stdout has beenexhausted when you get an EOF; the child has exitted when pclose returns. If youneed finer level control, you have to do the fork(), pipe(), exec*(), andwait*() calls yourself. --SM Ryan http://www.rawbw.com/~wyrmwif/Who''s leading this mob? ba*********** @ gmail.com 写道: 我正在尝试编写一个使用popen的程序,但我遇到了问题。我想要检测我用popen调用的程序是否已经结束。例如: #include< stdio.h> #include< sys / types.h> #include< unistd.h> .... snip ... 有人知道如何测试程序是否已经结束? I am trying to write a program which uses popen but I have a problem. I want to detect if the program I call with popen has ended. For example: #include <stdio.h> #include <sys/types.h> #include <unistd.h>.... snip ... Does anyone know how to test if the program has ended ? 既然你没有提到,除了< stdio.h> ;,存在于 便携式标准C语言中,答案是否定的,此处没有人知道。 但是,如果你要转到新闻组那些东西是局部的,可能用名称中的unix,答案可能是 不同。 - 一些信息链接: 新闻:news.announce.newusers http://www.geocities.com/nnqweb/ http://www.catb.org/~esr/faqs/smart-questions.html http://www.caliburn.nl/topposting.html http://www.netmeister.org/news/learn2quote.html Since nothing you mention, apart from <stdio.h>, exists in theportable standard C language, the answer is NO, nobody here knows.However, if you were to move to a newsgroup where those things aretopical, possibly with unix in its name, the answer might bedifferent. --Some informative links:news:news.announce.newusers http://www.geocities.com/nnqweb/ http://www.catb.org/~esr/faqs/smart-questions.html http://www.caliburn.nl/topposting.html http://www.netmeister.org/news/learn2quote.html ba *********** @ gmail.com 写道: I我正在尝试编写一个使用popen的程序,但我遇到了问题。我想要检测我用popen调用的程序是否已经结束。例如: #include< stdio.h> #include< sys / types.h> #include< unistd.h> int main() { FILE * pipe = NULL; int i = 0; while(1){ if(i == 10){ pipe = popen(" ls"," w"); } if(pipe!= NULL&& feof(pipe)){ printf(程序已结束\ n); } i ++ ; usleep(1000); } } 这不行。 有没有人知道如何测试该程序是否已经结束? I am trying to write a program which uses popen but I have a problem. I want to detect if the program I call with popen has ended. For example: #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { FILE *pipe = NULL; int i=0; while(1) { if (i == 10) { pipe = popen("ls", "w"); } if(pipe != NULL && feof(pipe)) { printf("Program has ended\n"); } i++; usleep(1000); } } This doesn''t work. Does anyone know how to test if the program has ended ? 可能,但因为它在clc中的主题很多人赢了' '$ 在这里讨论它,因为它不是C问题 - 标准C不知道同时运行的其他程序或关于功能的b $ tions像popen()。所以你最好在comp.unix.programmer这样的小组中问这些问题 ,这是关于主题的(但是 你也可能会得到告诉你几乎不能得到与popen()一起工作的并且你将需要使用fork() 和一个exec() - 函数族 - 并且你需要 " r"作为popen()的第二个参数当你使用ls时, ls应该为你创建输出阅读,不是吗?)。 但你的程序也存在与C相关的问题:feof() won'在你真正从文件中读到之前,不要告诉你任何合理的事情。 feof()无法预测EOF是否会在下一次读取时重新打开,之后只能告诉你 这就是为什么读取从文件失败。所以,即使你使用popen()产生的程序已经死了并关闭了它的管道边缘,只需调用feof()就不会帮助你了。因为 你从来没有读过任何东西,只有那个(加上清空管道) 可能导致feof()返回0以外的东西。 问候,Jens - \ Jens Thoms Toerring ___ Je *********** @ physik.fu-berlin.de \ __________________________ http://www.toerring.de 这篇关于popen:如何测试程序结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 10:48