C + + uses shell commands

Source: Internet
Author: User

#include <iostream>#include<stdio.h>#include<vector>#include<unistd.h>#include<sys/types.h>//Execute shell Command//executes a shell command, the output is stored line by row in Resvec, and the number of rows is returnedint32_t Myexec (Const Char*cmd, STD::VECTOR&LT;STD::string> &Resvec)    {resvec.clear (); FILE*pp = Popen (cmd,"R");//Build Pipelines    if(!PP) {        return-1; }    Chartmp[1024x768];//set an appropriate length to store each line of output     while(Fgets (TMP,sizeof(TMP), pp)! =NULL) {        if(Tmp[strlen (TMP)-1] =='\ n') {Tmp[strlen (TMP)-1] =' /';//Remove line break} resvec.push_back (TMP); } pclose (PP); //Close Pipe    returnresvec.size ();}intMainintargcConst Char*argv[]) {std::vector&LT;STD::string> *vect =NewSTD::VECTOR&LT;STD::string>(); pid_t PID=Getpid (); Char*cmd =New Char[1024x768]; sprintf (cmd,"ps-p%d-o ' Pid,pcpu,rss '", PID); Std::cout<< cmd <<Std::endl; int32_t a= Myexec (cmd, *vect); Std::cout<< a <<Std::endl; inti =0; intCount = vect->size ();  for(; i < count; i++) {Std::cout<< (*vect) [i] <<Std::endl; }        return 0;}

C + + uses shell commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.