您的位置:首页 > 运维架构 > Shell

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

2013-07-12 14:42 495 查看


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

分类: C/C++2013-02-06
10:16 55人阅读 评论(0) 收藏 举报

1、popen()

[cpp] view
plaincopy

#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: