Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
If I use a system call
system("wc -l ts.txt");
do I have to direct the output to a file
system("wc -l ts.txt > lines.txt");
and then read the file
or is there away to direct it directly to a variable.

thanks
 
See the man page for the popen() library function.

If that doesn't meet your needs, or you can't work out how to use it, then the answer to your question is "No": there is no way in C to direct the output of a sub-command directly to a variable*.


* other than writing such a function yourself, using existing functions such as system() or popen().
 
The man page for system() has a SEE ALSO entry for popen().

Often, an easy way to connect the dots is to read the man page for the first dot, and see where it points you.

For Xcode < 4, there's an "Open man page..." item under the Help menu. The page is presented as HTML, with clickable links for SEE ALSO entries.

For Xcode >= 4 I don't know.

And there's this tool:
http://www.bruji.com/bwana/
 
I looked at the help page but didn't follow through on the see also's. Popen did register.

corncob = popen("wc -l ts.txt", "r");
fgets(buf,100,corncob);
pclose(corncob);
sscanf(buf,"%d %s", &no_lines, junk);
printf("%d\n", no_lines);
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.