Data files consist of records which are broken down into fields. For example, the following payroll file has five records each of which has three fields:
Name | Hourly Rate | Hours Worked |
---|---|---|
Barry Bonds | 9.50 | 44 |
Mark McGwire | 12.00 | 34 |
Babe Ruth | 13.50 | 51 |
Roger Maris | 8.00 | 33 |
Sammy Sosa | 12.00 | 54 |
Each field has a name (name, hourly rate, and hours worked) and a data type. In this example, the name field is a string and the others are numeric.
The computer can be programmed to read from or write to files, and it typically reads or writes one record at a time. (When a record is read, it is copied from storage to memory, and when it is written, it is copied from memory to the file in storage).
We can also distinguish between sequential-access and random-access files. With a sequential-access file, the computer can only read or write records in order. In our example, it would first read Barry Bonds' record, then Mark McGwire's, etc. To get to Sammy Sosa it would have to read all the preceding records. With a random access file, the computer can jump directly to and read or write any record. You have also seen this distinction in recorded music. Songs on an audio tape must be accessed in sequence while with a CD you can jump to any track.
Sequential-access files are often referred to as stream or text files. This is because, you can think of them as streams of ASCII text characters, with a carriage-return and line-feed character at the end of each record.
Before a program can use a file, it must be opened. Programs will often have several files open at the same time, for example, reading data from one file, processing it, and writing the results to second file. When your program is finished using a file, you can close it, freeing up the memory it had been using.