• Note that to access a particular data item in a sequential file, you
need to read in all items in the file prior to the item of interest. This works
acceptably well for small data files of unstructured data, but for large, structured
files, this process is time-consuming and wasteful. Sometimes, we need to access
data in nonsequential ways. Files which allow nonsequential access are random
access files.
• To allow nonsequential access to information, a random access file
has a very definite structure. A random access file is made up of a number of
records, each record having the same length (measured in bytes). Hence, by knowing
the length of each record, we can easily determine (or the computer can) where
each record begins. The first record in a random access file is Record 1, not
0 as used in Visual Basic arrays. Each record is usually a set of variables, of
different types, describing some item. The structure of a random access file is:

• A good analogy to illustrate the differences between sequential files
and random access files are cassette music tapes and compact discs. To hear a
song on a tape (a sequential device), you must go past all songs prior to your
selection. To hear a song on a CD (a random access device), you simply go directly
to the desired selection. One difference here though is we require all of our
random access records to be the same length - not a good choice on CD’s!
• To write and read random access files, we must know the record length
in bytes. Some variable types and their length in bytes are:
Type Length (Bytes)
Integer 2
Long 4
Single 4
Double 8
String 1 byte per character
So, for every variable that is in a file’s record, we need to add up
the individual variable length’s to obtain the total record length. To ease
this task, we introduce the idea of user-defined variables.