Reputation: 14977
Is there a way to read in the result of a command-line in a C program?
For example, if I have the following code in a C program
sprintf(command, "cat input_file.txt | wc -l");
system(command);
can I get the result of the wc -l
and store it in a variable so I can use it in the same C program?
Upvotes: 2
Views: 549
Reputation: 25865
You can do
like
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num_line;
char *command="cat input_file.txt | wc -l";
FILE *p=popen(command,"r");
fscanf(p,"%d",&num_line);
printf("%d\n",num_line);
pclose(p);
return 0;
}
Upvotes: 1