【发布时间】:2010-11-29 14:35:49
【问题描述】:
正如标题所说,我不确定是否应该关闭使用 popen 打开的流。
我不确定的原因是因为每次我在使用 popen 打开的流上调用 pclose 时都会得到 -1 返回码。
如果我在此之后调用 perror,我会收到以下消息。
pclose:没有子进程
我在下面使用的代码基本上是运行命令并捕获其输出。我从最后一行得到错误(return pclose(fileListingStream);)
int executeCommand(char *command) {
//The Stream to read that will contain the output of the command
FILE *fileListingStream;
char path[PATH_MAX];
//Run the commmand in read mode
fileListingStream = popen(command,"r");
//Ensure that its not null before continuing
if (fileListingStream == NULL)
return EXIT_FAILURE;
//Get the data from the stream and then print to the the console
while (fgets(path, PATH_MAX, fileListingStream) != NULL)
printf("%s", path);
//Close the stream and return its return code
return pclose(fileListingStream);
}
【问题讨论】:
-
你的程序是否会忽略或处理
SIGCHLD?