Files, input, output

A few classes of the Kernel Library support file manipulation, input and output: STD_FILES , FILE, DIRECTORY and UNIX_FILE_INFO . For simple applications it suffices to use STD_FILES , but to understand the concepts better it is preferable to look first at the other two.

General files

FILE describes the notion of sequential file viewed as a data structure which fits in the general taxonomy of EiffelBase.

The class declaration defines files as unbounded sequences of characters. This means that you will find in FILE all the operations on sequential data structures that you have come to know and love by reading this documentation - at least, all that apply. Just as stacks and linked lists, files have put, extend, has, item and so on. More specific to files are the typed input and output operations. For output, you will find put_character, put_integer, put_real, put_double and put_string, as well as new_line. For input you will find read_integer and its co-conspirators.

Caution: Note the application to input features of the command-query separation principle.
The input features such as read_integer do not by themselves return a result; they set the values of queries such as last_integer. So the normal way to read is through two operations:
     my_file.read_integer
     new_value := my_file.last_integer

Queries are available to determine the status of a file, in particular exists, is_readable, is_executable, is_writable, is_creatable, is_closed, is_open_read and so on.

Caution: You will notice in the flat-short form that all these queries except the first have exists as a precondition. This precondition is good for efficiency since it saves an existence test - a relatively expensive operation - when you know that a certain file exists. But it also means that if you have any doubt about the file's existence you must use the queries in the style
if my_file.exists and then my_file.is_readable then ...

FILE is a deferred class. Various implementations are possible. A quite detailed one is PLAIN_TEXT_FILE, which adds many features for accessing reading and writing data from/to a file.

UNIX_FILE_INFO describes objects that contain internal information, such as protection mode and size, about a file.

The class DIRECTORY describes those files which are directories - nodes in the tree describing the file structure.

Basic input and output

Regardless of the operating system that you use, for simple input and output STD_FILES is sufficient. You may inherit from that class to gain direct access to its features; or you may declare an entity of type STD_FILES . But remember that a feature of this type is always available: io, from class ANY . Thanks to this feature you may include simple input and output in any class, with instructions such as io.put_string ("My message")

STD_FILES defines three default files through features input, output and error. These features are Once functions, so that the first reference to any one of them will automatically create the corresponding file descriptor and open the associated file.

To simplify the writing of common input and output operations, the most frequently used features of class FILE - for reading and writing integers, reals and so on, as discussed next - have been repeated in STD_FILES so as to apply to the default input and output. Procedure put_string in the example at the beginning of this section is typical: it writes its output on the standard output. More generally, STD_FILES has all the put_xxx, read_xxx and last_xxx features of FILE.