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命令的方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短