Hi, I have a question regarding the popen() function in C.
I would like to open a text file and count the "the" words, and this would do the job:
Code:
FILE* fin;
fin=popen("cat myfile.txt | grep -o -w 'the' |wc -w ","r");
char nr[5];
fgets(nr,sizeof nr,fin);
puts (nr);
pclose(fin);
Now i would like to read the file name,"myfile.txt", from the standard input as a string and pass it to the popen() function.
For example i would like to have something like:
Code:
char filename[20];
gets(filename);
FILE* fin;
fin=popen("cat filename | grep -o -w 'the' |wc -w ","w"))==NULL);
...
Is there any way i could do this?I know this doesn't work because Shell tries to open the file named 'filename'.
Thanks