How to store the result of calling a system command?

Hello. Help, please to understand the following question:
I need the program in C/C++, using the command in bash to calculate the number of lines in the file that contain the letter a. Write a program.
int main() {

 system("cat 1.txt | grep -c \"a\" ");
 return 0;

But I need to record the output of system("cat 1.txt | grep -c \"a\" "); into a variable within the program itself. Something like this int i = system("cat 1.txt | grep -c \"a\" ");. How can we do it?
July 9th 19 at 11:18
3 answers
July 9th 19 at 11:20
man popen
#include <stdio.h>

int main()
 int n;
 FILE *f = popen("grep -c \"a\" < 1.txt", "r");
 fscanf(f, "%d", &n);
not working - Oceane57 commented on July 9th 19 at 11:23
> does not work
: and more than two words to write -- in any way? What is not working? - demetrius.Bedn commented on July 9th 19 at 11:26
July 9th 19 at 11:22
a perversion, but
to override the console out stream string to run the command, change back out of ,to withdraw the place.
To start the process ,which will be out in your payp to go, etc.
A deep perversion. And alternatives system, no? - Oceane57 commented on July 9th 19 at 11:25
there,a separate process to run, but the solution is the same pipe and it read the result. - demetrius.Bedn commented on July 9th 19 at 11:28
July 9th 19 at 11:24
The best would be to add in the command passed to system(), redirect output to a file and then the file to read using fopen(). After all to remove a file using remove().

Find more questions by tags CC++Linux