3

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.

Community
  • 1
  • 1
James Ko
  • 32,215
  • 30
  • 128
  • 239
  • 5
    The `read` function is not a standard C function, only POSIX. If you want to be portable you should use `fread` (and associated functions) instead. – Some programmer dude Sep 02 '16 at 22:58
  • read(2) is POSIX. ISO C deals with standard streams only, as far as IO is concerned. If want to be super portable and efficient at the same time, you can disable buffering in `stdin` and use `fread`. – Petr Skocik Sep 02 '16 at 23:00
  • 3
    Support for `read` is deprecated *by Microsoft* on *Microsoft* platforms, not [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/functions/pread.html). – Andrew Henle Sep 02 '16 at 23:00
  • 1
    Using MS as a reference for C is a bad idea. They refuse to support the C standard since 17 years now. They also dropped POSIX support, so they are also a bad reference for POSIX related issues. And the citation apparently is from some C++ domumentation, which is a different language. Also a bad resource for C. – too honest for this site Sep 03 '16 at 01:20
  • @AndrewHenle @yellowantphil @Olaf and others, I was not quoting 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. – James Ko Sep 03 '16 at 01:43
  • @JoachimPileborg Thanks for your helpful comment! Please consider posting it as an answer so other beginners won't have to re-ask :) – James Ko Sep 03 '16 at 01:45
  • Could be a duplicate of by http://stackoverflow.com/questions/544662/is-there-any-ordinary-reason-to-use-open-instead-of-fopen, except this question appears to focus exclusively on `stdin`. –  Sep 03 '16 at 01:55
  • @hvd I guess you could say that. However, before I was searching for "is read part of c89 standard" on Google, and that didn't turn up anything, so this new question could help people who type that in whereas the question you linked to focuses on `fopen`. – James Ko Sep 03 '16 at 01:59
  • `read` is POSIX. `read(2)` is a man page reference. – Kaz Sep 03 '16 at 02:01

1 Answers1

2

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.

rici
  • 234,347
  • 28
  • 237
  • 341