In Linux, you can use C ++ to obtain command line output content through pipelines.
Reprint please explain the source: http://blog.csdn.net/cywosp/article/details/8980898
When using C ++, sometimes we can use the pipeline method to obtain the output content of the executed command. The procedure is as follows:
First, you need to determine the format of the content output by the command you run (the format of the content output by the same command in different operating systems may be different ). Here is an example: I want to get the temporary storage space of the/var directory, measured in KB:
[Root @ localhost/] # Du-sk/var
13532132var
[Root @ localhost/] # Du-sk/var | awk-F ''' {print $1 }'
13532132
From the above output, we can know that the/var directory currently uses 13532132kb, so how can we change the value to ourProgramMedium? For simplicity, I implemented the following functions:
Int64_t calculatedirusedcapacity (const STD: string & directoryname) {file * fstream = NULL; char buff [32]; memset (buff, '\ 0', sizeof (buff )); STD: String cmd = "Du-SK" + directoryname; cmd + = "| awk-F ''' {print $1 }'"; // return the value if (null = (fstream = popen (CMD. c_str (), "R") {return 0;} If (null = fgets (buff, sizeof (buff), fstream) {pclose (fstream ); return 0;} STD: strstream SS; SS <Buff; int64_t usedcapacity = 0; SS> usedcapacity; pclose (fstream); // close pipe return usedcapacity ;}