UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

Chapter 9.  Reading and writing with files

9.1.  Introduction

The C language provides a set of library functions to perform input and output (I/O) operations. Those functions can read or write any type of data to files. Before going into detail with the I/O functions, we first have to understand some basic concepts.

9.1.1.  What is a file?

In C, a file can refer to a disk file, a terminal, a printer, etc. That is, a file represents a concrete device with which you want to exchange information. Before you can operate on the file, you have to open that file. After you finish the information exchange with it, you have to close that opened file.

9.1.2.  What is a stream?

The data flow you transfer from a program to a file, or the other way around, is called a stream, which is a series of bytes (or characters). Unlike a file, which targets a specific I/O device, a stream is device independent. In other words, all streams have the same behavior regardless of whatever device they are associated with. Therefore, you can perform I/O operations to any type of file by simply associating a stream to a file.

There are two formats of streams. The first one is the text stream, which consists of text lines. A line is a sequence of characters terminated by a newline character ( '\n' or '\r\n'). The second format is the binary stream, which is a series of bytes that represent internal data such as numbers, structures or arrays. Binary streams are primarily used for non-textual data, where the appearance of the content inside the file is nor important (it does not care if they are not seen as plain text).

9.1.3.  Buffered I/O

A buffer is a memory area that is temporarily used to store data before it is sent to its destination. With the help of buffers, the operating system can improve efficiency by reducing the number of accesses to an I/O device (that is, a file).

Access to a disk or other I/O device is generally much slower than direct access to memory. For that reason, if several operations E/S are sent to a buffer, they are kept in memory until it is time to save, or commit, the new data to the actual device (this process is known as flushing the buffer).

By default, all I/O streams are buffered.

9.1.4.  I/O model behaviour

Since the smallest object that can be represented in C is a character, access to a file is permitted at any character (or byte) boundary. Any number of characters can be read or written from a movable point, known as the file position indicator. The characters are read, or written, in sequence from this point, and the position indicator moved accordingly. The position indicator is initially set to the beginning of a file when it is opened, but can also be moved explicitly (not just because a read or write has been performed).