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 好听的元旦简短祝福语