Answer the question
In order to leave comments, you need to log in
How to store the result of a system command call?
Hello. Please help me understand the following question:
In a C / C ++ program, using a command in bash, I need to count the number of lines in a file containing the letter a . I am writing a program.
#include<stdio.h>
int main() {
system("cat 1.txt | grep -c \"a\" ");
return 0;
}
Answer the question
In order to leave comments, you need to log in
man popen
#include <stdio.h>
int main()
{
int n;
FILE *f = popen("grep -c \"a\" < 1.txt", "r");
fscanf(f, "%d", &n);
pclose(f);
...
}
perversion, but,
redefine console out with a string stream, run a command, change out back, output a line.
Run a process that will have an out in your pipe, etc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question