1. 程式人生 > 其它 >2.9 popen函式

2.9 popen函式

popen函式的應用

#include <stdio.h>
#include <unistd.h>
int main()
{
        FILE *fd;
        char ret[1024] = {0};
        fd = popen("ps","r");	
        int nread = fread(ret,1,1024,fd);		//將獲取的內容存放在ret快取區中
        printf("num = %d byte,ret:\n%s",nread,ret);
        return
0; }

popen比system在應用中的好處:可以獲取執行的結果
而,system函式只能存放在終端中

在這裡插入圖片描述