Вы находитесь на странице: 1из 28

Chapter 10

Sequential Data Files

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

10.1 An Introduc/on to Files


A le is a collec=on of informa=on that has been assigned a name
and stored separately from the program that created it.
A le may contain programs or data.
There are two main types of les:

Text les
Binary les

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Classica/on of Files
By content:

Text, readable by humans


ASCII characters only
Binary, readable only by the computer
ASCII data plus special codes

By method of access:
Sequen2al les contain records that must be read in the order in which they were created
Similar to an audio or VCR tape

Direct Access les contain records that can be accessed in any order.
Similar to a CD or DVD

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Text Files or Binary Files?


Binary les
Used by most opera=ng system les, program les, and data les produced by
applica=ons

Advantages of text les


Easier to create by using a text editor
Can be displayed on screen or printed without any special soTware
Universal: virtually any computer system can interpret their contents without
special soTware

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Records and Fields


One le may be broken up into groups of related data, called records
Records contain data items and each data item in a record is called a eld
Example: A business might keep a le for customers. The data might be
customer name, address, phone number. A record of one customer consists of
the 3 elds:
the customers name
the customers address
the customers phone number.

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Records and Fields in a Data File


PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Records and Fields in a Data File


The elds are separated from one
another by commas
Each record is terminated by a special
symbol: <CR>
The rst two records of this le would
look as follows:
R. Abrams,86,64,73,84<CR>
J. Chavez,94,87,83,90<CR>

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Sequen/al and Direct-Access Files


Data les can be divided into two other categories:
Sequen2al les
contain records that must be processed in the order in which they were created
are accessed in linear fashion
For example, to print the 50th record in a sequen=al le, must rst read (or scan) the 49 records that
precede it

Direct-access les (or random-access les)


each record can be accessed independently of the rest
loca=ng a data item is like nding a certain track on a DVD

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Crea/ng a Sequen/al File


Open the le. Specify:
External name: the full name of the le on disk.
Internal name: the name by which the le will be known in the program
File mode: the purpose of the le: Input or Output

Write data to the le, or


Read data from the le
Close the le
Saves the le and puts an end-of-file marker (EOF) aTer the last record if the le was created in
the program
Closes the le if the le is an Input le

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Example: Opening and Closing a File


If we issue the command:
Open grades for Output As NewFile
grades is the external name
NewFile is the internal name
The mode is Output

If we issue the command:


Close NewFile
An EOF (end-of-le) marker is placed at the end of the le

The le is closed
The le is saved with the external name grades

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Example: Reading a File


To open a le to be read:
Open grades for Input As GradeFile
Read the internal lename and the elds/variables:
Read GradeFile, Name, Score
Read records within a loop (EOF() is discussed later):

While NOT EOF(GradeFile)
Read GradeFile, Name, Score
...
End While
Close GradeFile

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Example: Crea/ng a Sequen/al File


1 Declare Student As String

2 Declare Score As Integer


3 Open grades For Output As NewFile
4 Write Enter students name and test score.
5 Write Enter 0 for both when done.
6 Input Student, Score
7 While Student != 0
8

Write NewFile, Student, Score

Write Enter students name and test score.

10

Write Enter 0 for both when done.

11

Input Student, Score

12 End While
13 Close NewFile

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

The EOF() Func/on


To terminate the input or output process of a loop that works with data in a data le, and to force an exit from
the loop, most programming languages contain an end-of-le, EOF() func=on:

EOF(InternalName)
The EOF() func2on
may appear in the test condi=on of any loop or selec=on structure
has the value true if the end of the le InternalName has been reached -- i.e. if the le
pointer is located at the end-of-le (EOF) marker
otherwise the value is false
The use of the EOF() func=on is illustrated in the example in the next slide

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Example: Using the EOF() Func/on


1
2
3
4
5
6
7
8

Declare Student As String


Declare Score As Integer
Open grades For Input As GradeFile
While NOT EOF(GradeFile)
Read GradeFile, Student, Score
Write Student + + Score
End While
Close GradeFile

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

10.2 Modifying a Sequen/al File


The most common opera=ons on sequen=al les are:
dele2ng an exis=ng record
changing an exis=ng record
inser2ng or adding a new record

Read the le, one record at a =me, rewri=ng each record to a temporary or
scratch le un=l reaching the one to be modied.
The mode is Input for the original le and Output for the scratch le.
If modifying an exis=ng record, make the change and write the new version to
the scratch le.

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Modifying a Sequen/al File (con/nued)


If dele=ng an exis=ng record, skip over the record to be deleted.
If inser=ng a new record, read down to the proper loca=on, then
write the new record.
If the loca=on is to be the end of the le, read to the end and write
the record.
Close the Input and Output les.
Copy the scratch le onto the original le.

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Example: Modifying a Sequen/al le

Dele/ng a record from a le that contains student names and one test score:
1

Declare Student As String

Declare DeleteName As String

Declare Score As Integer

Open grades For Input As GivenFile

Open scratch For Output As TempFile

Write Enter name of student to be deleted:

Input DeleteName

While NOT EOF(GivenFile)

Read GivenFile, Student, Score

10

If Student != DeleteName Then

11
12

Write TempFile, Student, Score


End If

13

End While

14

Close GivenFile, TempFile

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Example: Modifying a Sequen/al le (continued)


The updated le on the previous slide is now named TempFile. To restore grades as the name of the
updated le, the records from the scratch le must be copied to grades as follows:
Open grades for Output As TargetFile
Open scratch for Input As SourceFile
While NOT EOF(SourceFile)
Read SourceFile, Student, Score
Write TargetFile, Student, Score
End While
Close SourceFile, TargetFile
Note: the scratch le s=ll contains the informa=on but it doesnt maber. The next =me
you open the scratch le to modify another le, the old contents will be erased.

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Example: Modifying One Field in One Record


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Declare Name As String


Declare NewScore As Integer
Open grades For Input As GivenFile
Open scratch For Output As TempFile
Write Enter the name of the student:
Input Name
Write Enter new test score:
Input NewScore
While NOT EOF(GivenFile)
Read GivenFile, Student, Score
If Student == Name Then
Write TempFile, Student, NewScore
Else
Write TempFile, Student, Score
End If
End While
Close GivenFile, TempFile
Copy the file scratch onto the file grades
PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Example: Inser/ng a Record into a Sequen/al File


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

Declare NewName As String


Declare NewScore As Integer
Open grades For Input As GivenFile
Open scratch For Output As TempFile
Write Enter name and score for new student:
Input NewName, NewScore
Set Inserted = 0
While (NOT EOF(GivenFile)) AND (Inserted == 0)
Read GivenFile, Student, Score
If NewName < Student Then
Write TempFile, NewName, NewScore
Set Inserted = 1
End If
Write TempFile, Student, Score
End While
If Inserted == 0 Then
Write TempFile, NewName, NewScore
End If
While NOT EOF(GivenFile)
Read GivenFile, Student, Score
Write TempFile, Student, Score
End While
Close GivenFile, TempFile
Copy scratch onto grades

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Using Arrays in File Maintenance


Some=mes it is beber to load a le into arrays in the computers internal memory.
Works if the le is small enough to t into available memory
Also good if there are many changes to be made to the le
Internal memory works fast and can be more ecient than using a scratch le

General procedure:
1. Open the given le for Input (to be read from)
2. Read le records into parallel arrays, one array for each eld
3. Close the le (so that it can later be opened for Output)
4. Make the desired modica=ons to the arrays
5. Open the le for Output (which erases all the original data in this le)
6. Write the contents of the arrays (the modied data) to the given le
7. Close this le.
PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Declare Student[100] As String


Declare Test1[100] As Integer
Pseudocode for Using Arrays for
Declare Test2[100] As Integer
File Maintenance
Declare Count As Integer
Open grades For Input As DataFile
Set Count = 0
While NOT EOF(DataFile)
Read DataFile, Student[Count], Test1[Count]
Set Count = Count + 1
End While
Close DataFile
Open grades For Output As DataFile
For (K = 0; K < Count; K++)
Write Enter Test 2 score for + Student[K]
Input Test2[K]
Write DataFile, Student[K], Test1[K], Test2[K]
End For
Close DataFile

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

10.3 Merging Sequen/al Files


Open the two given les, File1 and File2, for Input.
Open the le that will hold the merged records, File3 for Output
Successively Read records from File1 and File2
If the current record for File1 precedes that of File2, then write the
File1 record to File3; otherwise, write the File2 record to File3
Close the three les

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

General Pseudocode for Merging Files


Read the first record from each file
While (NOT EOF(File1)) AND (NOT EOF(File2))
Compare the current records for File1 and File2
If the File1 record precedes the File2 record Then
Write the File1 record to File3
Read another record from File1
Else
Write the File2 record to File3
Read another record from File2
End If
End While
Read the remaining records, if any, in File1 and write them to File3
Read the remaining records, if any, in File2 and write them to File3

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Example: The Big Merger


A company wants to merge two payroll les (payroll1 and payroll2) into a single le.

Suppose that each record in these les has the following form:
Employee_number (Integer) employee_name (String) rate_of_pay
(Float)
We will assume that the records are ordered (in increasing order) by employee number and
that the last record in each le is 0, 0, 0.0. We will merge these two les into a new le
called payroll. The variables are declared below and the rest of the program is on the next
slides
1
2
3

Declare Number1, Number2 As Integer


Declare Name1, Name2 As String
Declare Rate1, Rate2 As Float

Con=nued on next slide

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

Open payroll1 For Input As File1


Open payroll2 For Input As File2
Open payroll For Output As File3
Read File1, Number1, Name1, Rate1
Read File2, Number2, Name2, Rate2
While (Number1 != 0) AND (Number2 != 0)
If Number1 < Number2 Then
Write File3, Number1, Name1, Rate1
Read File1, Number1, Name1, Rate1
Else
Write File3, Number2, Name2, Rate2
Read File2, Number2, Name2, Rate2
End If
End While
While Number1 != 0
Write File3, Number1, Name1, Rate1
Read File1, Number1, Name1, Rate1
End While
While Number2 != 0
Write File3, Number2, Name2, Rate2
Read File2, Number2, Name2, Rate2
End While
Write File3, 0, 0, 0.0
Close File1, File2, File3

The Big Merger (con/nued)

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Con=nued on next slide

The Big Merger (con/nued)


To understand this program beber, walk through the pseudocode using les with the indicated records:
payroll1:
payroll2:
115, Art, 11.50
120, Dan, 14.00
130, Ben, 12.25
125, Eva, 15.50
135, Cal, 13.75
0, 0, 0.0
0, 0, 0.0
Then, aTer the merge, payroll should contain the following records:
115, Art, 11.50
120, Dan, 14.00
125, Eva, 15.50
130, Ben, 12.25
135, Cal, 13.75
0, 0, 0.0
PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

10.4 Control Break Processing


When we need subtotals (perhaps for a report), we use a technique called Control
Break Processing.
This technique will do something (for example, calculate a subtotal) depending on the
value of a control variable. When a change occurs in the value of this variable, a break
occurs that results in something happening in the program:
For example:
A program accepts monthly sales amounts from the user and computes monthly subtotals
When month changes from January to February, Januarys sales are subtotaled and printed.
Then the program starts adding up Februarys sales.
The cycle repeats when February changes to March and so on

PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE