Connecting Tech Pros Worldwide Help | Site Map

Using "wc" inside C program

  #1  
Old November 16th, 2008, 06:45 PM
Snaggy
Guest
 
Posts: 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
  #2  
Old November 16th, 2008, 06:45 PM
Malcolm McLean
Guest
 
Posts: n/a

re: Using "wc" inside C program



"Snaggy" <l.cioria@gmail.comwrote in message news
Quote:
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

  #3  
Old November 16th, 2008, 06:55 PM
Richard Tobin
Guest
 
Posts: n/a

re: Using "wc" inside C program


In article <525be7b5-f6d1-4826-9668-cd0909e65446@1g2000prd.googlegroups.com>,
Snaggy <l.cioria@gmail.comwrote:
Quote:
>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.
  #4  
Old November 16th, 2008, 06:55 PM
Richard Tobin
Guest
 
Posts: n/a

re: Using "wc" inside C program


In article <u-Kdnb92xZu39L3UnZ2dnUVZ8vKdnZ2d@bt.com>,
Malcolm McLean <regniztar@btinternet.comwrote:
Quote:
>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.
  #5  
Old November 16th, 2008, 07:25 PM
Snaggy
Guest
 
Posts: n/a

re: Using "wc" inside C program


On Nov 16, 7:40*pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
Quote:
"Snaggy" <l.cio...@gmail.comwrote in message news
Quote:
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..
>
Quote:
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
  #6  
Old November 16th, 2008, 11:35 PM
Nate Eldredge
Guest
 
Posts: n/a

re: Using "wc" inside C program


Snaggy <l.cioria@gmail.comwrites:
Quote:
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.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Using "wc" inside C program eerpini answers 4 November 16th, 2008 11:55 PM