The C language uses the cmd command and gets the output method

Source: Internet
Author: User

Go from Http://blog.csdn.net/hxh129/article/details/8000205C language use cmd command and get output method in practice, we sometimes need to use C language to invoke cmd command, and get the result of execution, Here is a simple example.   #include <stdio.h> //Description: The Execmd function executes the command and stores the results in the result string array  //parameter: cmd indicates the command to execute// Result is an array of execution results stored in the string//function execution successfully returned 1, failed to return 0  int execmd (char* cmd,char* result) {    char BUFFER[128];                          //Defining Buffers                             file* pipe = _ Popen (cmd, "R");            //Open the pipe and execute the command      if (!pipe)           return 0;                       //Return 0 indicates a run failure              while (!feof (pipe)) {     if (fgets (buffer, $, pipe)) {             //pipe output to result               strcat (Result,buffer);        }    }     _pclose (pipe);                             //closing the pipe      return 1;                                  //return 1 indicates successful run  }  int main () {    char result[1024*4]= "";                    //defines a string array that holds the result      if (1==execmd ( "ipconfig", result) {        printf (result);     }     system ("pause");                           //pause to view results  }   In addition, Microsoft's MSDN documentation has more detailed instructions, interested friends can look at:/http MSDN.MICROSOFT.COM/EN-US/LIBRARY/96AYSS4B (v=vs.80). aspx.

C language uses the cmd command and gets the output method

Related Article

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.