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