19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

(a) Often a large portion of the file must be read in order to find one specific<br />

item.<br />

(b) An individual item of the file cannot be changed or deleted easily.<br />

Another type of file, known as a r<strong>and</strong>om-access file, has neither of the disadvantages<br />

of sequential files; however, r<strong>and</strong>om-access files typically use more<br />

disk space, require greater effort to program, <strong>and</strong> are not flexible in the variety<br />

<strong>and</strong> format of the stored data. R<strong>and</strong>om-access files are discussed in Section 8.<br />

2. Consider the sequential file shown in Figure 7-1 at the end of Example 2. This<br />

file is said to consist of three records of two fields each. A record holds all the<br />

data about a single individual. Each item of data is called a field. The three<br />

records are<br />

“Barbra”, 1942<br />

“Ringo”, 1940<br />

“Sylvester”, 1946<br />

<strong>and</strong> the two fields are<br />

name field, year of birth field<br />

7.2 USING SEQUENTIAL FILES<br />

In addition to being accessed for information, sequential files are regularly updated by modifying<br />

certain pieces of data, removing some records, <strong>and</strong> adding new records. These tasks<br />

can be performed most efficiently if the files are first sorted.<br />

■ SORTING SEQUENTIAL FILES<br />

The records of a sequential file can be sorted on any field by first reading the data into parallel<br />

arrays <strong>and</strong> then sorting on a specific array.<br />

EXAMPLE 1<br />

The following program sorts the sequential file YOB.TXT of the previous section by year of birth.<br />

Private Sub cmdSort_Click()<br />

Dim numPeople As Integer<br />

‘Sort data from YOB.TXT file by year of birth<br />

numPeople = NumberOfRecords(“YOB.TXT”)<br />

ReDim nom(1 To numPeople) As String<br />

ReDim yearBorn(1 To numPeople) As Integer<br />

Call ReadData(nom(), yearBorn(), numPeople)<br />

Call SortData(nom(), yearBorn(), numPeople)<br />

Call ShowData(nom(), yearBorn(), numPeople)<br />

Call WriteData(nom(), yearBorn(), numPeople)<br />

End Sub<br />

Private Function NumberOfRecords(filespec As String) As Integer<br />

Dim nom As String, yearBorn As Integer<br />

Dim n As Integer ‘Used to count records<br />

n = 0<br />

Open filespec For Input As #1<br />

Do While Not EOF(1)<br />

Input #1, nom, yearBorn<br />

n = n + 1<br />

Loop<br />

Close #1<br />

Using Sequential Files 223

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

Saved successfully!

Ooh no, something went wrong!