Reading and Writing

The UNIX kernel readies a file for I/O by giving you a file descriptor, which is returned by open(2) or creat(2). The file descriptor might represent an I/O device, a socket or, most often, a regular file. The I/O semantics vary somewhat, depending on what it is that your program is interacting with. This will be noted in a few places as you are introduced to the system calls for I/O.

Introducing read(2) and write(2)

These are perhaps the most basic of all UNIX I/O system calls. Their function synopsis is as follows:

#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

ssize_t read(int fd,void *buf,size_t nbytes);

ssize_t write(int fd,const void *buf,size_t nbytes);

The read(2) and write(2) calls take the same ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.