Previous topic |
Ada Home Page |
Index
Direct access files
Direct access files are structured binary files, in which is it possible
to go directly to a desired record.
To enable this, a file index is used.
- index = position for next I/O operation
When it can be manipulated:
- set index, then do I/O
- access records in any order
- read or write
- direct access file
The DIRECT_IO package is used
- not text files
- direct and sequential I/O both possible
An example program shows the
use of a direct access file in a program dealing with bank accounts.
A direct access file is appropriate here because only one of the many
records in the file is updated.
Using direct access files
General file management
I/O library instantiation
- similar to SEQUENTIAL_IO
- record type must precede library instantiation
variable definition
- as for text files, sequential files
open/close files
- INOUT_FILE allows reading and writing
- file mode can also be IN_FILE or OUT_FILE
procedures and functions that use the file index:
- SIZE - how many records in the file?
- INDEX - what is the current value of the file index?
- SET_INDEX - set the value of the file index
Input/Output
relative to index:
SET_INDEX (accfile, recordnum);
WRITE (accfile, rec);
specific location:
WRITE (accfile, rec, recordnum);
file index is automatically incremented after I/O
- sequential access possible
while INDEX(accfile) <= 100 loop
READ (accfile, rec);
end loop;
Previous topic |
Ada Home Page |
Index
c-lokan@adfa.oz.au / 23 Feb 96