Connecting Tech Pros Worldwide Help | Site Map

Re: Using "wc" inside C program

  #1  
Old November 16th, 2008, 09:45 PM
eerpini
Guest
 
Posts: n/a
yes it is supposedly easier to use a temporary file, but is it possible
to use popen() for the given instance ?
how can the required read and write achieved using popen(), when as you
mentioned popen() allows only one at a time ?

-- Satish
On Sun, 2008-11-16 at 18:56 +0000, Richard Tobin wrote:
Quote:
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
  #2  
Old November 16th, 2008, 10:25 PM
CBFalconer
Guest
 
Posts: n/a

re: Re: Using "wc" inside C program


eerpini wrote:
Quote:
>
yes it is supposedly easier to use a temporary file, but is it
possible to use popen() for the given instance ? how can the
required read and write achieved using popen(), when as you
mentioned popen() allows only one at a time ?
Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/ (taming google)
<http://members.fortunecity.com/nnqweb/ (newusers)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
  #3  
Old November 16th, 2008, 10:35 PM
Richard Tobin
Guest
 
Posts: n/a

re: Re: Using "wc" inside C program


In article <1226871686.25699.1.camel@localhost.localdomain> ,
eerpini <eerpini@gmail.comwrote:
Quote:
>yes it is supposedly easier to use a temporary file, but is it possible
>to use popen() for the given instance ?
You can use popen() to send it the input through a pipe, with the
result going to a file you read later. Or you can put the input in a
file, and use popen() to read the result. You can't use popen() to
get a pipe for both input and output.

To use a pipe for both input and output, you have to do something much
more complicated, such as using pipe(), fork(), and exec() to set up
the process, and select() to handle the reading and writing. It's
not worth the trouble in most cases.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
  #4  
Old November 16th, 2008, 11:05 PM
eerpini
Guest
 
Posts: n/a

re: Re: Using "wc" inside C program


Quote:
Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:
sorry for that , went through the links,
Thanks

  #5  
Old November 16th, 2008, 11:55 PM
CBFalconer
Guest
 
Posts: n/a

re: Re: Using "wc" inside C program


eerpini wrote:
Quote:
>
Quote:
>Please do not top-post. Your answer belongs after (or intermixed
>with) the quoted material to which you reply, after snipping all
>irrelevant material. See the following links:
>
sorry for that , went through the links,
Good for you. That's why I try to respond to top-posters early.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using "wc" inside C program Snaggy answers 5 November 16th, 2008 11:35 PM