欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C++/Php/Python 語言執(zhí)行shell命令的方法(推薦)

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

編程中經(jīng)常需要在程序中使用shell命令來簡化程序,這里記錄一下。

1. C++ 執(zhí)行shell命令

#include <iostream>
#include <string>
#include <stdio.h>

int exec_cmd(std::string cmd, std::string &res){
  if (cmd.size() == 0){  //cmd is empty 
    return -1;
  }

  char buffer[1024] = {0};
  std::string result = "";
  FILE *pin = popen(cmd.c_str(), "r");
  if (!pin) { //popen failed 
    return -1;
  }

  res.clear();
  while(!feof(pin)){
    if(fgets(buffer, sizeof(buffer), pin) != NULL){
      result += buffer;
    }
  }

  res = result;
  return pclose(pin); //-1:pclose failed; else shell ret
}

int main(){
  std::string cmd = "ls -ial";
  std::string res;

  std::cout << "ret = " << exec_cmd(cmd, res) << std::endl;
  std::cout << res << std::endl;

  return 0;
}

2. Php執(zhí)行shell命令

<?php
  $cmd = "wc -l ./test.php";
  exec($cmd, $output, $code);

  echo $code."\n";
  print_r($output);
?>

3. Python執(zhí)行shell命令

import commands

status, output = commands.getstatusoutput('ls -lt')

print status
print output

以上這篇C++/Php/Python 語言執(zhí)行shell命令的方法(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持我們。

上一篇:C++中實現(xiàn)矩陣的加法和乘法實例

欄    目:C語言

下一篇:C語言基礎(chǔ)之格式化輸出控制長度

本文標(biāo)題:C++/Php/Python 語言執(zhí)行shell命令的方法(推薦)

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1665.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有