C + + uses shell commands

Source: Internet
Author: User

#include <iostream>
#include <stdio.h>
#include <vector>
#include <unistd.h>
#include <sys/types.h>


//execute Shell commands
//Executes a shell command, the output is stored line by row in Resvec and returns the number of rows
int32_t myexec (const char *CMD, std:: Vector<std::string> &resvec) {
    resvec.clear ();
    FILE *pp = popen (cmd, "R"); Set up Pipeline
    if (!pp) {
        return-1;
   }
    char tmp[1024];//set an appropriate length to store each line of output
    while (fgets ( TMP, sizeof (TMP), pp)! = NULL) {
        if (Tmp[strlen (TMP)-1] = = ' \ n ') {
&nbs p;           Tmp[strlen (TMP)-1] = ' + '; Remove line break
       }
        Resvec.push_back (TMP);
   }
    pclose (PP);//close pipe
    return resvec.size ();
}

int main (int argc, const char * argv[]) {

std::vector<std::string> *vect = new std::vector<std::string> ();
pid_t pid = Getpid ();
Char *cmd = new char[1024];
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;

int i = 0;
int count = Vect->size ();
for (; i < count; i++) {
std::cout<< (*vect) [i] <<std::endl;
}

return 0;
}

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 returned
int32_t Myexec (ConstChar *cmd, STD::VECTOR&LT;STD::String> &Resvec) {
Resvec.clear ();
FILE *pp = Popen (cmd,"R");//Build Pipelines
if (!PP) {
Return-1;
}
Char tmp[1024];//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 ();
}

int main (int argc,Constchar *Argv[]) {

STD::VECTOR&LT;STD::String> *vect =New STD::VECTOR&LT;STD::String>();
pid_t PID =Getpid ();
Char *cmd =Newchar[1024];
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;

int i = 0;
int count = 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.