One program calling another and using stdout in Unix

Started by
6 comments, last by clum 20 years, 3 months ago
Is there a way having a program (C++ or Java) which runs another program and redirects standard output into a string? Actually, what I really want to do is to write a program which fines the length of a file and uses that information, but I figured the best way would be to take advantage of the unix command "wc". Thank you in advanced. The official zorx website
Zorx (a Puzzle Bobble clone)Discontinuity (an animation system for POV-Ray)
Advertisement
Try popen/pclose().
My stuff.Shameless promotion: FreePop: The GPL god-sim.
If you just want the length of the file, you might want to just use the stat() or fstat() system calls.
he might mean length of a file in terms of words, not bytes.

in which case, you''re not really writing a program (strict definition) you want a shell script. a simple

#!/bin/bash
echo -n "The file $1 has "
cat $1 | wc | awk ''{ print $1 }'' | echo -n
echo -n " words."

works nicely as

$ ./countwords textfile.txt
The File textfile.txt has 1234 words.
$



Actually, I did want the size in bytes. fstat worked well (thank you, SiCrane). However, I find it annoying that its so easy to use the output of a program in bash or perl (with `backquotes`), while its so complicated in C.

The official zorx website
Zorx (a Puzzle Bobble clone)Discontinuity (an animation system for POV-Ray)
C is compiled.
OpenGL Revolutions http://students.hightechhigh.org/~jjensen/
quote:Original post by 31337
<sigh> C is compiled.

So what ? This is just a question of syntax, with libraries to back it up. It has nothing to do with the program being compiled or interpreted.

[edited by - Fruny on January 4, 2004 9:48:04 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by Fruny
quote:Original post by 31337
<sigh> C is compiled.

So what ? This is just a question of syntax, with libraries to back it up. It has nothing to do with the program being compiled or interpreted.


Precisely. The syntax for reading the stdout of other commands ought to be easier in shell scripts since they do that sort of thing more often than something like C.
My stuff.Shameless promotion: FreePop: The GPL god-sim.

This topic is closed to new replies.

Advertisement