Obtain command line output results in C program

Source: Internet
Author: User

In the C program, the system function can run the command line, but only the int type return value of the command line can be obtained, and the display result cannot be obtained. For example, system ("ls") can only get 0 or not 0. If you want to get the LS execution result, you need to complete it through the pipeline. First, use popen to open a command line pipeline, and then use fgets to obtain the content transmitted by the pipeline, that is, the result of running the command line.

An example of running on Linux is as follows:

Void executecmd (const char * cmd, char * result) <br/>{< br/> char buf_ps [1024]; <br/> char PS [1024] = {0}; <br/> file * PTR; <br/> strcpy (Ps, CMD ); <br/> If (PTR = popen (Ps, "R "))! = NULL) <br/>{< br/> while (fgets (buf_ps, 1024, PTR )! = NULL) <br/>{< br/> strcat (result, buf_ps); <br/> If (strlen (result)> 1024) <br/> break; <br/>}< br/> pclose (PTR); <br/> PTR = NULL; <br/>}< br/> else <br/> {<br/> printf ("popen % s error/N", PS ); <br/>}< br/>}

In this Code, the CMD parameter is the command line to be executed, and the result is the command line running result. The input cmd command should be in the format of... 2> & 1, so as to read standard errors.

 

In Windows, it is relatively troublesome. You need to use the createprocessw function to start a new process to execute the CMD command. For the example in windows, see the code that calls md5sum.exe to obtain the MD5 value of the file:

Int getfilemd5w (const tchar * filefullpath, char * md5key) <br/>{< br/> tchar szfilenamew [max_path_length] = {0 }; // save the file name <br/> tchar szfilepathw [max_path_length] = {0}; // Save the path <br/> tchar szcmdlinew [max_path_length] = {0 }; // save Command Line Information <br/> char buffer [max_path_length] = {0}; // save command line output <br/> tchar * Pos = NULL; <br/> DWORD bytesread = 0; <br/> If (wcslen (filefullpath)> max_path_length) <br/> return false; <br/> Wcscpy (szfilepathw, filefullpath); <br/> int I = 0; <br/> while (szfilepathw [I]! = 0) <br/> {<br/> If (szfilepathw [I] = _ T ('/')) <br/> szfilepathw [I] = _ T ('//'); <br/> I ++; <br/>}< br/> If (Pos = wcschr (szfilepathw, '//') = NULL) // find '/' At the rightmost of the file path <br/>{< br/> return false; <br/>}</P> <p> wcscpy (szfilenamew, pos + 1); // obtain the file name <br/> * Pos = 0; // obtain the file path <br/> If (wcslen (szfilenamew) = 0 | wcslen (szfilepathw) = 0 | md5key = NULL) // check whether the file name or path size is appropriate <br/>{< br/> return false; <br/>}< br/> Wsprintf (szcmdlinew, l "cmd.exe/C md5sum/" % S/"", szfilenamew); // Command Line Information <br/> // eg: cmd.exe/C md5sum "for text.txt" <br/> security_attributes SA = {0}; <br/> handle hread = NULL, hwrite = NULL; // set the MPs queue read/write handle <br/> SA. nlength = sizeof (security_attributes); <br/> SA. lpsecuritydescriptor = NULL; <br/> SA. binherithandle = true; <br/> If (! Createpipe (& hread, & hwrite, & SA, 0) // create an MPS queue <br/>{< br/> return false; <br/>}< br/> startupinfo Si = {0}; <br/> process_information Pi = {0}; <br/> Si. CB = sizeof (startupinfo); <br/> getstartupinfo (& Si); <br/> Si. hstderror = hwrite; // <br/> Si. hstdoutput = hwrite; // <br/> Si. wshowwindow = sw_hide; <br/> Si. dwflags = startf_useshowwindow | startf_usestdhandles; <br/> // For the key steps, refer to the parameter Meanings of the CreateProcess function. Msdn <br/> If (! Createprocessw (null, szcmdlinew <br/>, null, null, true, null, null, szfilepathw, & Si, π) // note that szfilepathw (path of the file) is used here) as the third to last parameter <br/>{< br/> closehandle (hwrite); <br/> closehandle (hread); <br/> return false; <br/>}< br/> waitforsingleobject (Pi. hprocess, infinite); // wait until md5sum ends <br/> // close process and thread handles. <br/> closehandle (Pi. hprocess); // close the main thread of the new process <br/> closehandle (Pi. hthread); // close the New Process <br //> Closehandle (hwrite); // close the write handle of the pipeline <br/> readfile (hread, buffer, max_path_length, & bytesread, null ); // read the running result of md5sum from the MPs queue <br/> closehandle (hread); // close the read handle of the MPs queue <br/> If (null! = Strstr (buffer, "md5sum") // If md5sum appears in the running result, most of the operations fail <br/>{< br/> // trace (buffer ); <br/> return-2; <br/>}< br/> else if (! Strnicmp (buffer, "No such file:", strlen ("No such file :"))) // The specified file cannot be found. <br/>{< br/> // trace (buffer); <br/> return-1; <br/>}< br/> If (strlen (buffer) <32) // The result is smaller than 32 characters, the MD5 value is not obtained <br/>{< br/> // trace (buffer); <br/> return false; <br/>}< br/> strncpy (md5key, buffer, 32); // MD5 value obtained successfully <br/> strcat (md5key, "/0 "); <br/> return true; <br/>}

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.