POSIX-ANSI equivalent?

R

rantingnews

Deal all,

It is a trivial question, i know. I have few lines of POSIX code that
I don't know if I can express them with simple ANSI C. These are
simple:

read(0, buffer, len);
write(1, buffer, len);

I have tried to simply translate them to

fread(buffer, 1, len, stdin);
fwrite(buffer, 1, len, stdout);

but the program (started by a server) doesn't work. It seems that my
program (started by a server, not by me directly) when using read/
write works, while with fread/fwrite expects something from stdin/
stdout (I suppose, and it is actually what those calls would do). In
practice, it would seem that on read(0, ...) stdin is redirected
somehow, although I don't know anything about it.

Am I misinterpreting read/write ?

Thanks!
 
K

Keith Thompson

It is a trivial question, i know. I have few lines of POSIX code that
I don't know if I can express them with simple ANSI C. These are
simple:

read(0, buffer, len);
write(1, buffer, len);

I have tried to simply translate them to

fread(buffer, 1, len, stdin);
fwrite(buffer, 1, len, stdout);

but the program (started by a server) doesn't work. It seems that my
program (started by a server, not by me directly) when using read/
write works, while with fread/fwrite expects something from stdin/
stdout (I suppose, and it is actually what those calls would do). In
practice, it would seem that on read(0, ...) stdin is redirected
somehow, although I don't know anything about it.

Am I misinterpreting read/write ?

read and fread do have some differences in behavior; likewise fread
and fwrite. The main difference is probably buffering.

Your read and fread calls should both read from stdin, and your write
and fwrite calls should both write to stdout. If stdin is redirected,
it should apply equally to read and fread.

You may be running into some subtlety of the read and write functions.
The folks in comp.unix.programmer can probably help you with that.
 
N

Nate Eldredge

Deal all,

It is a trivial question, i know. I have few lines of POSIX code that
I don't know if I can express them with simple ANSI C. These are
simple:

read(0, buffer, len);
write(1, buffer, len);

I have tried to simply translate them to

fread(buffer, 1, len, stdin);
fwrite(buffer, 1, len, stdout);

but the program (started by a server) doesn't work. It seems that my
program (started by a server, not by me directly) when using read/
write works, while with fread/fwrite expects something from stdin/
stdout (I suppose, and it is actually what those calls would do). In
practice, it would seem that on read(0, ...) stdin is redirected
somehow, although I don't know anything about it.

Am I misinterpreting read/write ?

Not that I can see.

One guess is that the buffering done by fread/fwrite is screwing things
up somehow. Try adding

setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);

at the start of your program (before any reading or writing), and see if
this changes anything. If it still doesn't work, then maybe you could
explain more precisely in what way it doesn't work.
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top