Random- Access Files
In the previous sections, you learned how to read
and write data to a sequential file. In this section,
you will learn how to access random files.
A random file is a file whose data you can read
or write in any order without having to read or
write all other data.
For example, you can write student records to a random
file and then read one or more record later in any
order. Before we discuss about random files, we will
first discuss about user-defined data type. Random
access files often read and write data records, and
VB's user defined data types let you define data items
that look exactly like the records you need to write
to (and read from) the random file.
The following statement opens a file for random access:
Open "Random.txt" For Random As #1
Two statements are used for random access files:
Put # and Get #.
Put [#]filenumber, [recnumber], varname
Get [#]filenumber, [recnumber], varname
These statements use a record number. Record numbers
begin with 1. The variable you read or write can be
of any data type- even an array or a user defined
|| The freedom to handle any type
of variable as a single unit is one of the most
powerful features of random access files.