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命令的方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。