Search code examples
cposixfread

Is read(2) part of the C89 specification?


I'm somewhat new to C. I am wondering, is the read(2) function part of the C89 specification, or just the POSIX one? I ask since I was trying to figure out the return type for read, and the man pages in places such as here say it's ssize_t. However, ssize_t 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 fread: http://port70.net/~nsz/c/c89/c89-draft.html#4.9.8.1

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 read 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.


Solution

  • 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.