C + + to get cmd output characters through pipe pipes

Source: Internet
Author: User

#include <stdio.h>
#include <iostream>
#include <string>
using namespace Std;

Description: The Execmd function executes the command and stores the result in the results string array
Parameter: cmd indicates the command to execute
Result is an array of strings that execute the resulting store
function execution successfully returns 1, failure returns 0
int Execmd (char* cmd,char* result) {
Char buffer[128]; Defining buffers
file* pipe = _popen (cmd, "R"); Open the pipeline and execute the command
if (!pipe) return 0; Returning 0 indicates a failed run

while (!feof (pipe)) {
if (fgets (buffer, line, pipe)) {//pipe is output to result
strcat (Result,buffer);
}
}
_pclose (pipe); Close pipe
return 1; Returning 1 indicates a successful run
}

int main () {
Char result[1024*4]= ""; Defines a string array that holds the result
if (1==execmd ("rasdial", result)) {
printf (result);
cout<<result;
}
System ("pause"); Pause to view results
}

C + + to get cmd output characters through pipe pipes

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.