stdio.h vs unistd.h I/O

Hi guys.

To work with physical files, sockets, pipes, ... which library is good? stdio or unistd
stdio.h functions perform buffering and rationally should be better than unistd.h routines. but i am wondering why all UNIX programming books use unistd.h routines for almost all types of I/O operations?

stdio.h and unistd.h are header files, not libraries. stdio.h is the header for stream/buffered I/O(like printf()). unistd.h is the header for the POSIX API(like read()). You probably see the use of low level functions like read() because the examples are working directly with file descriptors - ie: sockets. stream I/O uses file pointers. The choice to use buffered/non-buffered I/O depends on what the application is doing.

so for which applications it is good to use buffered I/O and for which it is better to use unbuffered I/O?

As a general rule (and this is only my opinion) it's generally better to use the C standard library functions in stdio.h where you can (i.e. for file I/O) and then use the POSIX standard functions in unistd.h etc. when you need to do I/O on file descriptors for sockets and such.

Generally, you may as well try and use the most portable interfaces you can (i.e. stdio.h in this case).

1 Like

Thank you very much.

Depends on your definition of "portable". The windows stdio implementation is awful.

1 Like

Yeah, but their unistd implementation just ain't there...

In general, fully buffered I/O will run quicker, I think.

1 Like