C++/Php/Python 语言执行shell命令的方法(推荐)
编程中经常需要在程序中使用shell命令来简化程序,这里记录一下。
1.C++执行shell命令
#include#include #include intexec_cmd(std::stringcmd,std::string&res){ if(cmd.size()==0){//cmdisempty return-1; } charbuffer[1024]={0}; std::stringresult=""; FILE*pin=popen(cmd.c_str(),"r"); if(!pin){//popenfailed return-1; } res.clear(); while(!feof(pin)){ if(fgets(buffer,sizeof(buffer),pin)!=NULL){ result+=buffer; } } res=result; returnpclose(pin);//-1:pclosefailed;elseshellret } intmain(){ std::stringcmd="ls-ial"; std::stringres; std::cout<<"ret="< 2.Php执行shell命令
3.Python执行shell命令
importcommands status,output=commands.getstatusoutput('ls-lt') printstatus printoutput以上这篇C++/Php/Python语言执行shell命令的方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短