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

Thursday, January 17, 2013 , , , 0 Comments

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];
if(fgets(buffer, 128, pipe) != NULL){}
buffer[strlen(buffer)-1] = '\0';