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:
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.Code:FILE* fin; fin=popen("cat myfile.txt | grep -o -w 'the' |wc -w ","r"); char nr; fgets(nr,sizeof nr,fin); puts (nr); pclose(fin);
For example i would like to have something like:
Is there any way i could do this?I know this doesn't work because Shell tries to open the file named 'filename'.Code:char filename; gets(filename); FILE* fin; fin=popen("cat filename | grep -o -w 'the' |wc -w ","w"))==NULL); ...