执行shell命令并读取执行结果——popen()的用法

1、popen()

#include<stdio.h>
void call(){
        FILE* fp = popen("ls", "r");
        char buffer[1024];
        int bytes_read = fread(buffer, 1, sizeof(buffer), fp);
        pclose(fp);
        if(bytes_read ==0){
                printf("read error! \n");
                return;
        }
        else{
                buffer[ bytes_read ] = 0;
                printf("cmd result is \n%s\n",buffer);
        }
}

void main(void){
        call();
        return;
}


你可能感兴趣的:(执行shell命令并读取执行结果——popen()的用法)