question about writing c code supports pipe in

C

Carson

Hi,

How to write c code such that it supports pipe in?

i.e.,

echo "ABCD" | a.out

(how to write a c-code which generates the binaries a.out that can take the
pipe in input for further processing?)

thank you.

Carson
 
J

Joona I Palaste

Carson said:
How to write c code such that it supports pipe in?

echo "ABCD" | a.out
(how to write a c-code which generates the binaries a.out that can take the
pipe in input for further processing?)

Technically, pipes are an operating system feature and thus off-topic on
comp.lang.c.
However...
<OT>On most operating systems, for example all sensible Unices, these
kinds of pipes as you mention above are handled at stdio stream level.
This means that the first command's stdout is automatically connected to
the second program's stdin, and so on.
This means that you don't have to do *ANYTHING* to make your program
support pipe in. Just write your program using stdio streams like any C
textbook says, and the OS will automatically handle pipe in for you.
</OT>
 
C

ChokSheak Lau

Carson said:
Hi,

How to write c code such that it supports pipe in?

i.e.,

echo "ABCD" | a.out

(how to write a c-code which generates the binaries a.out that can take the
pipe in input for further processing?)

thank you.

Carson

read the stdin of `a.out'.

chok
 
K

Kenneth Brody

Carson said:
Hi,

How to write c code such that it supports pipe in?

i.e.,

echo "ABCD" | a.out

(how to write a c-code which generates the binaries a.out that can take the
pipe in input for further processing?)

If your program gets its input from stdin, you don't have to do anything.
If your program doesn't use stdin, then you need to rewrite it so it does.
 
T

Tiago Quelhas

Kenneth said:
If your program gets its input from stdin, you don't have to do anything.
If your program doesn't use stdin, then you need to rewrite it so it does.

Actually, wouldn't this break on non-Unix systems since stdin is opened
as a text stream? (I'm assuming a.out is binary data and thinking about
the potential end-of-line translations in other systems).

I don't intend to flame, only ask if I'm seeing it wrong.
 
E

E. Robert Tisdale

Carson said:
How to write c code such that it supports pipe in?

i.e.,

echo "ABCD" | a.out

(how to write a c-code which generates the binaries a.out
that can take the pipe in input for further processing?)

It sounds like you want to write a *filter*.
A filter accepts input from stdin and emits output to stdout.
The "pipe" is provide by your operating system shell.
You don't need to do anything special.
Just use stdin for input and stdout for output.
 
K

Keith Thompson

Tiago Quelhas said:
Actually, wouldn't this break on non-Unix systems since stdin is
opened as a text stream? (I'm assuming a.out is binary data and
thinking about the potential end-of-line translations in other
systems).

a.out is the name of the executable program, not a data file. (It's
the default name of the executable generated by most Unix C compilers;
it was originally an abbreviation of "assembler output".)
 

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top