Using "wc" inside C program

S

Snaggy

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
 
R

Richard Tobin

Snaggy said:
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
 
R

Richard Tobin

Malcolm McLean said:
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
 
S

Snaggy

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.

popen's perfect
thanks
 
N

Nate Eldredge

Snaggy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top