我正在尝试在我的 C 程序中执行其他程序。我的第一次尝试是使用 popen。当我尝试从中读取时,pipe
我只得到 1 个字节的回复,而在 buf 中什么也没有。我不确定这背后的原因。
弹出示例:
#include<stdio.h>
#include<unistd.h>
int main(int argc, char* argv[])
{
FILE* pipe;
if ((pipe=(FILE*)popen("./test.php","r"))==NULL)
printf("this is not working\n");
char buf[1024] = {'\0'};
int fd=fileno(pipe);
int bytes = read(fd, buf, 1024);
printf("bytes read %d\n", bytes);
printf("The program: %s\n", buf);
if(pclose(pipe)<0)
printf("not working\n");
return 0;
}
例子
#!/usr/bin/php
<?php
echo "THIS IS A TEST THAT WORKED\n";
?>
输出:
bytes read 1
The program:
ls的输出:
ls -l test.php
-rwxr-xr-x+ 1 tpar44 user 62 Nov 10 14:42 test.php
对此的任何帮助将不胜感激!谢谢!