Executing shell command in c/c++ and parsing the output

17.1.13
Often we need to execute some shell command and get the output of that shell command in c.
system will simply execute the shell command but ther is no way using it for fetching its output.
below is the way where we can execute the command and also get its output into a c/c++ buffer.
FILE* pipe = popen("your shell command here", "r");
if (pipe)
{
char buffer[128];
while(!feof(pipe))
{
if(fgets(buffer, 128, pipe) != NULL){}
}
pclose(pipe);
buffer[strlen(buffer)-1] = '\0';
}

No comments:

Post a Comment