char ret[1024]; char *DoSysCmd(char * cmdline){ FILE *fp; char line[32]; char cmdtmp[256]; memset(cmdtmp,0x00,256); sprintf(cmdtmp,"%s 2>&1",cmdline);//stderr是2,把2转向1 memset(ret,0x00,sizeof(ret)); fp = popen(cmdtmp, "r"); while ( fgets( line, sizeof(line), fp)) { sprintf(ret ,"%s%s",ret,line); } pclose(fp); return ret; }
使用例子
show_Info("检查是否存在可移动存储设备......");
result=DoSysCmd("cat /proc/scsi/scsi");
if (0==strcmp(result,"Attached devices: none/n"))
{
show_Info("对不起,没有发现任何设备!");
return;
}