執行過程
#include <stdio.h>
void print_all(FILE *stream)
{
int c;
while ((c = getc(stream)) != EOF)
putchar(c);
}
int main(void)
{
FILE *stream;
/* call netstat command. netstat is available for Windows and Linux */
if ((stream = popen("netstat", "r")) == NULL)
return 1;
print_all(stream);
pclose(stream);
return 0;
}
該程式通過 popen() 執行一個程序( netstat ) 並讀取程序的所有標準輸出,並將其回顯到標準輸出。
注意: popen() 在標準 C 庫中不存在,但它是 POSIX C的一部分 )