Previous topic | Next topic | Ada Home Page | Index

Sequential files

With sequential files, each element (usually a record) must be processed in turn. If you want to process a particular record, you must read every record in the file in turn until you get to the one you want.

Sequential files are appropriate when you want to process most (or all) of the records in a file every time you run your program.

The mechanics of using sequential files are very similar to the mechanics of using text files:

Unlike text files, sequential files cannot be created or viewed with a text editor. They must be created and displayed under program control.

An example program demonstrates all of the things listed above and described in detail below.


Using sequential files

Instantiate I/O library

To use sequential files, you need a WITH statement that imports the SEQUENTIAL_IO library.

You must define the element type first, and can then instantiate the SEQUENTIAL_IO library. This creates an I/O library of routines for manipulating a sequential file of that type of element.

example:

        with SEQUENTIAL_IO;
	procedure demo is

	    ...

            type details is record
                name    : string (1..30);
                address : string (1..80);
                age     : natural;
            end record;

            package details_io is new SEQUENTIAL_IO (details);

declare file variables

        file_variable : package.FILE_TYPE;

Example:

        details_file : details_io.FILE_TYPE;

OPEN, CREATE, CLOSE

As for text files
        OPEN   (olddetails, IN_FILE,  "details.dat");
        CREATE (newdetails, OUT_FILE, "newdetails.dat");

Input/output

SEQUENTIAL_IO provides procedures for I/O:

Note:

Example:

        while not END_OF_FILE (olddetails) loop
            READ  (olddetails, one_person);
            ...
            WRITE (newdetails, one_person);
        end loop;


Creating/displaying sequential files

You cannot use a text editor to create or display a structured file.

Use specially-written programs.


Previous topic | Next topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 23 Feb 96