#include<stdio.h> #include<stdlib.h> #include<unistd.h> int main(void) { char ret[1024]={0}; FILE *fp; fp = popen("ps","r");//调用可执行文件,并传参r int nread = fread(ret,1,1024,fp);//读取fp中的数据给ret,一次读一个字节,读1024次,nread为实际读取的次数 printf("read ret %d byte, ret= %s\n",nread,ret); return 0; } ~
执行显示为:
read ret 141 byte, ret= PID TTY TIME CMD
24775 pts/1 00:00:00 bash
27849 pts/1 00:00:00 a.out
27850 pts/1 00:00:00 sh
27851 pts/1 00:00:00 ps