正如标题所说,我不确定是否应关闭使用popen打开的流.
我不确定的原因是因为每次我在使用popen打开的流上调用pclose时,我得到一个-1返回代码.
如果我之后打电话给perror,我收到以下消息.
pclose: No Child Processes
我在下面使用的代码基本上是运行一个命令并捕获它的输出.我从最后一行得到错误(返回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);
}