James Ko James Ko - 1 year ago 88
C Question

Is read(2) part of the C89 specification?

I'm somewhat new to C. I am wondering, is the

function part of the C89 specification, or just the POSIX one? I ask since I was trying to figure out the return type for
, and the man pages in places such as here say it's
. However,
is only required in POSIX, not regular C according to this question. This MSDN page seems to confirm my suspicions, since it says

This POSIX function is deprecated. Use the ISO C++ conformant _read instead.

I got a hold of the draft of the C89 standard, and there is no mention of read in the table of contents. There is, however, a mention of
: http://port70.net/~nsz/c/c89/c89-draft.html#

So then is it better to use e.g.
fread(buf, 1, sizeof(buf), stdin)
rather than
read(STDIN_FILENO, buf, sizeof(buf))
for reading from standard input?

edit: Sorry for the confusion. I did not quote MSDN to suggest that
was deprecated, merely to show that it is indeed a part of the POSIX standard (it mentions "POSIX function") as opposed to the C standard.

Answer Source

read() is not and never has been standard C, so if you want to write portable code which reads from files, don't use it; use fread().

On the other hand, there may be things you want to do on a Posix system which are not portable, such as use pipes and sockets. In that case, go ahead and use the Posix interfaces.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download