popen结合fread进行子进程内容的输出

popen可以读取子进程中运行的结果并且输出出来
而execl,system不行

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main(){
    
    
	int nread = 0;
	char ret[1024]={
    
    0};
	FILE *fp;
	fp = popen("ps","r");
	nread = fread(ret,1,1024,fp);
	printf("read %d byte,ret= %s\n",nread,ret);
}

猜你喜欢

转载自blog.csdn.net/weixin_41679960/article/details/114984928