By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

Using "wc" inside C program

P: n/a
Or any other standar unix program..
I want to pass something to a pipe for wc (to count words) and read
the result into a variable..

For now I put an intermediate result into a file and then open the
file and read it..
thanks
bye
Nov 16 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Snaggy" <l.******@gmail.comwrote in message news
Or any other standar unix program..
I want to pass something to a pipe for wc (to count words) and read
the result into a variable..

For now I put an intermediate result into a file and then open the
file and read it..
thanks
system() is the ANSI -standard way of calling an external program,
However Unix machines usually offer a fucntion called popen() for pipe open,
which may be what you are looking for.
However since it is non-standard current topicality conventions state that I
can't tell you much about it.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Nov 16 '08 #2

P: n/a
In article <52**********************************@1g2000prd.go oglegroups.com>,
Snaggy <l.******@gmail.comwrote:
>Or any other standar unix program..
I want to pass something to a pipe for wc (to count words) and read
the result into a variable..

For now I put an intermediate result into a file and then open the
file and read it..
Reading and writing to another program through pipes is, in general, a
recipe for deadlock. In the case of "wc" this wouldn't be a problem,
but if you were using something like "tr" it would. To make it work
requires some kind of asynchronous (or select()ed) i/o, multiple
threads, or arbitrary buffering in the operating system. Consequently
the unix popen() call only provides for you to either read or write,
not both.

For this simple case, your solution of using a temporary file is
probably the easiest, requiring little unix-specific knowledge.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Nov 16 '08 #3

P: n/a
In article <u-******************************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>system() is the ANSI -standard way of calling an external program,
However Unix machines usually offer a fucntion called popen() for pipe open,
which may be what you are looking for.
However since it is non-standard current topicality conventions state that I
can't tell you much about it.
system() requires almost as much non-C-standardness as popen(), since
you still need how to get a legal temporary file name, and the syntax
for redirection.

In any case, the OP's description suggests that he is already using
popen(). You can't do what he's asking for with it.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Nov 16 '08 #4

P: n/a
On Nov 16, 7:40*pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
"Snaggy" <l.cio...@gmail.comwrote in message news
Or any other standar unix program..
I want to pass something to a pipe for wc (to count words) and read
the result into a variable..
For now I put an intermediate result into a file and then open the
file and read it..
thanks

system() is the ANSI -standard way of calling an external program,
However Unix machines usually offer a fucntion called popen() for pipe open,
which may be what you are looking for.
However since it is non-standard current topicality conventions state that I
can't tell you much about it.

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm
popen's perfect
thanks
Nov 16 '08 #5

P: n/a
Snaggy <l.******@gmail.comwrites:
Or any other standar unix program..
I want to pass something to a pipe for wc (to count words) and read
the result into a variable..

For now I put an intermediate result into a file and then open the
file and read it..
For something as simple as wc, it's probably faster, easier, and more
portable just to do the word count yourself. Here's one possibility,
which I hereby release into the public domain.

#include <stddef.h>
#include <ctype.h>

size_t count_words(const char *s) {
int inword = 0;
size_t words = 0;
for ( ; *s; s++) {
if (!inword && !isspace(*s)) {
/* started a word */
inword = 1;
words++;
} else if (inword && isspace(*s)) {
inword = 0;
}
}
return words;
}

Counting lines and characters is of course even easier.
Nov 16 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.