#### Archived

This topic is now archived and is closed to further replies.

# One program calling another and using stdout in Unix

This topic is 5257 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
Try popen/pclose().

##### Share on other sites
If you just want the length of the file, you might want to just use the stat() or fstat() system calls.

##### Share on other sites
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.
\$

##### Share on other sites
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

C is compiled.

##### Share on other sites
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]

##### Share on other sites
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.

• 10
• 17
• 9
• 14
• 41
• ### Forum Statistics

• Total Topics
631067
• Total Posts
2997734
×