15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

266<br />

Storing Fixed Size Data in Random-Access Files<br />

Figure 8-2:<br />

The fixed<br />

size of each<br />

record<br />

makes it<br />

easy to<br />

identify the<br />

physical<br />

location of<br />

each record<br />

in a file.<br />

Each record takes up a fixed<br />

amount of space inside a file.<br />

FirstName LastName Age Phone FirstName LastName<br />

Age Phone FirstName LastName Age Phone<br />

Writing data<br />

To store (write) data to a random-access file, define a variable that represents<br />

your records like this:<br />

Type PeopleRecord<br />

LastName as String * 20<br />

FirstName as String * 20<br />

Age as Integer<br />

Phone as String * 12<br />

End Type<br />

Dim Contact as PeopleRecord<br />

After you define a variable to represent your record, store data in the<br />

Contact variable, such as<br />

Contact.LastName = “Smith”<br />

Contact.FirstName = “Joey”<br />

Contact.Age = 28<br />

Contact.Phone = “310-123-1234”<br />

The next step is to create a random-access file by defining a filename like<br />

this:<br />

Open “MyFile.dat” <strong>for</strong> Random as #1 Len = Len(Contact)<br />

This code opens (creates) a random-access file named Myfile.dat, identified<br />

by the number 1 (#1) and divided into chunks defined by the size or<br />

length (Len) of the Contact variable (record).<br />

You can choose any number to represent the random-access file. Using a<br />

number, such as #1 or #8, is much easier than typing the entire filename out<br />

each time you need to identify which file to use again.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!