tags:

views:

81

answers:

2

I am doing some file IO with c code. I have a particular pattern in my file. I can verify this by a shell command cat abc.txt | grep abc | wc -l. When I execute the same command using System(), it give proper output, but I have no clue how can I get its output into a variable and compare it in my c code itself.

I tried looking into man pages which suggest using WEXITSTATUS(). This actually returns the status of execution and not output.

F1 !!

+7  A: 

You don't want system(3) for that. Try popen(3) and friends.

Ignacio Vazquez-Abrams
+1 here is an example of this http://www.metalshell.com/source_code/23/Popen.html
DRL
dont they have the same problem, calling the shell ?
Tom
@Tom i would have thought that the only major problem is if the program requested for execution is not in the $PATH of the calling process
DRL
My background is more on Windows. Is there any quick start book on Linux programming?
vrrathod
http://www.amazon.com/Linux-Application-Development-Michael-Johnson/dp/0201308215
Ignacio Vazquez-Abrams
He doesn't want either. Parsing the output of `ls` is a horrible idea. He wants `opendir` and `readdir` and `qsort`.
R..
well the original command I wanted was 'cat abc.txt | grep abc | wc -l'. I just happen to put ls in title, just to make it short :)
vrrathod
+1  A: 

What grep and wc are doing are reading the STDIN file handle as part of the pipe | operator.

Paul Nathan