Next: Exercises Up: Input and Output (I/O) Previous: Command line input

Low Level I/O

This form of I/O is UNBUFFERED - each read/write request results in accessing disk (or device) directly to fetch/put a specific number of bytes.

There are no formatting facilities - we are dealing with bytes of information.

This means we are now using binary (and not text) files.

Instead of file pointers we use low level file handle or file descriptors which give a unique integer number to identify each file.

To Open a file use:

int open(char *filename, int flag, int perms) - this returns a file descriptor or -1 for a fail.

The flag controls file access and has the following predefined in fcntl.h:

O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_WRONLY + others see online man pages or reference manuals.

perms - best set to 0 for most of our applications.

The function:

creat(char *filename, int perms)

can also be used to create a file.

int close(int handle) - close a file

int read(int handle, char *buffer, unsigned length)

int write(int handle, char *buffer, unsigned length)

are used to read/write a specific number of bytes from/to a file (handle) stored or to be put in the memory location specified by buffer.

The sizeof() function is commonly used to specify the length.

read and write return the number of bytes read/written or -1 if they fail.


Dave.Marshall@cm.cf.ac.uk
Wed Sep 14 10:06:31 BST 1994