Archived

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

clum

One program calling another and using stdout in Unix

Recommended Posts

clum    217
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 this post


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

Share this post


Link to post
Share on other sites
C-Junkie    1099
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 this post


Link to post
Share on other sites
clum    217
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

Share this post


Link to post
Share on other sites
Fruny    1658
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 this post


Link to post
Share on other sites
Doc    586
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.

Share this post


Link to post
Share on other sites