我想在我的C程序中執行其他程序。我第一次嘗試是用popen。當我嘗試從pipe
讀取時,我只收到1個字節的回覆,而且buf中沒有任何內容。我不確定這背後的原因。Popen和execl不給出用戶定義的程序的輸出
POPEN例如:
#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;
}
PHP示例
#!/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
任何幫助,將不勝感激! 謝謝!
仍然沒有看到任何問題,請問您可以添加輸出嗎? – iabdalkader
@mux我也沒有,這就是爲什麼我現在很困惑 – tpar44
這可能看起來很愚蠢,但你重新編譯的代碼:)? – iabdalkader