php popen not close,使用popen时必须关闭流

"这篇博客探讨了在C程序中使用popen函数执行命令并捕获输出时遇到的问题。作者注意到在调用pclose时返回-1,并通过perror获取到错误信息"NoChildProcesses"。代码示例展示了如何执行命令、读取输出以及关闭流的过程。问题可能源于进程管理或资源释放的不正确操作。"
摘要由CSDN通过智能技术生成

正如标题所说,我不确定是否应关闭使用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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值