2. 10.1 An Introduc/on to Files
A
file
is
a
collec=on
of
informa=on
that
has
been
assigned
a
name
and
stored
separately
from
the
program
that
created
it.
A
file
may
contain
programs
or
data.
There
are
two
main
types
of
files:
◦Text
files
◦Binary
files
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
3. Classifica/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
files
contain
records
that
must
be
read
in
the
order
in
which
they
were
created
◦ Similar
to
an
audio
or
VCR
tape
◦ Direct
Access
files
contain
records
that
can
be
accessed
in
any
order.
◦ Similar
to
a
CD
or
DVD
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
4. Text Files or Binary Files?
Binary
files
◦ Used
by
most
opera=ng
system
files,
program
files,
and
data
files
produced
by
applica=ons
Advantages
of
text
files
◦ 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
5. Records and Fields
ØOne
file
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
field
ØExample:
A
business
might
keep
a
file
for
customers.
The
data
might
be
customer
name,
address,
phone
number.
A
record
of
one
customer
consists
of
the
3
fields:
◦ the
customer’s
name
◦ the
customer’s
address
◦ the
customer’s
phone
number.
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
7. Records and Fields in a Data File
Ø
The
fields
are
separated
from
one
another
by
commas
Ø
Each
record
is
terminated
by
a
special
symbol:
<CR>
Ø
The
first
two
records
of
this
file
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
8. Sequen/al and Direct-‐Access Files
Data
files
can
be
divided
into
two
other
categories:
Sequen2al
files
◦ 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
file,
must
first
read
(or
scan)
the
49
records
that
precede
it
Direct-‐access
files
(or
random-‐access
files)
◦ each
record
can
be
accessed
independently
of
the
rest
◦ loca=ng
a
data
item
is
like
finding
a
certain
track
on
a
DVD
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
9. Crea/ng a Sequen/al File
Ø Open the
file.
Specify:
◦ External
name:
the
full
name
of
the
file
on
disk.
◦ Internal
name:
the
name
by
which
the
file
will
be
known
in
the
program
◦ File
mode:
the
purpose
of
the
file:
Input or
Output
Ø Write data
to
the
file,
or
Ø Read
data
from
the
file
Ø Close
the
file
◦ Saves
the
file
and
puts
an
end-of-file
marker
(EOF)
aTer
the
last
record
if
the
file
was
created
in
the
program
◦ Closes
the
file
if
the
file
is
an
Input
file
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
10. 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-‐file)
marker
is
placed
at
the
end
of
the
file
◦ The
file
is
closed
◦ The
file
is
saved
with
the
external
name
grades
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
11. Example: Reading a File
Ø
To
open
a
file
to
be
read:
Open “grades“ for Input As GradeFile
Ø
Read
the
internal
filename
and
the
fields/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
12. 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 student’s 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
9 Write “Enter student’s 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
13. The EOF() Func/on
To
terminate
the
input
or
output
process
of
a
loop
that
works
with
data
in
a
data
file,
and
to
force
an
exit
from
the
loop,
most
programming
languages
contain
an
end-‐of-‐file,
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
file
InternalName
has
been
reached
-‐-‐
i.e.
if
the
file
pointer
is
located
at
the
end-‐of-‐file
(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
14. Example: Using the EOF() Func/on
1 Declare Student As String
2 Declare Score As Integer
3 Open “grades” For Input As GradeFile
4 While NOT EOF(GradeFile)
5 Read GradeFile, Student, Score
6 Write Student + “ “ + Score
7 End While
8 Close GradeFile
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
15. 10.2 Modifying a Sequen/al File
Ø
The
most
common
opera=ons
on
sequen=al
files
are:
◦
dele2ng
an
exis=ng
record
◦
changing
an
exis=ng
record
◦
inser2ng
or
adding
a
new
record
Ø Read
the
file,
one
record
at
a
=me,
rewri=ng
each
record
to
a
temporary
or
scratch
file
un=l
reaching
the
one
to
be
modified.
Ø
The
mode
is
Input
for
the
original
file
and
Output
for
the
scratch
file.
Ø
If
modifying
an
exis=ng
record,
make
the
change
and
write
the
new
version
to
the
scratch
file.
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
16. 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
file,
read
to
the
end
and
write
the
record.
Ø
Close
the
Input
and
Output
files.
Ø
Copy
the
scratch
file
onto
the
original
file.
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
17. PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Example: Modifying a Sequen/al file
Dele/ng a record from a file that contains student names and one test score:
1 Declare Student As String
2 Declare DeleteName As String
3 Declare Score As Integer
4 Open “grades” For Input As GivenFile
5 Open “scratch” For Output As TempFile
6 Write “Enter name of student to be deleted:”
7 Input DeleteName
8 While NOT EOF(GivenFile)
9 Read GivenFile, Student, Score
10 If Student != DeleteName Then
11 Write TempFile, Student, Score
12 End If
13 End While
14 Close GivenFile, TempFile
18. PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Example: Modifying a Sequen/al file (continued)
The updated file on the previous slide is now named TempFile. To restore ”grades” as the name of the
updated file, the records from the ”scratch” file 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
file
s=ll
contains
the
informa=on
but
it
doesn’t
maber.
The
next
=me
you
open
the
scratch
file
to
modify
another
file,
the
old
contents
will
be
erased.
19. PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Example: Modifying One Field in One Record
1 Declare Name As String
2 Declare NewScore As Integer
3 Open “grades” For Input As GivenFile
4 Open “scratch” For Output As TempFile
5 Write “Enter the name of the student: “
6 Input Name
7 Write “Enter new test score: “
8 Input NewScore
9 While NOT EOF(GivenFile)
10 Read GivenFile, Student, Score
11 If Student == Name Then
12 Write TempFile, Student, NewScore
13 Else
14 Write TempFile, Student, Score
15 End If
16 End While
17 Close GivenFile, TempFile
18 Copy the file scratch onto the file grades
20. PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Example: Inser/ng a Record into a Sequen/al File
1 Declare NewName As String
2 Declare NewScore As Integer
3 Open “grades” For Input As GivenFile
4 Open “scratch” For Output As TempFile
5 Write “Enter name and score for new student:”
6 Input NewName, NewScore
7 Set Inserted = 0
8 While (NOT EOF(GivenFile)) AND (Inserted == 0)
9 Read GivenFile, Student, Score
10 If NewName < Student Then
11 Write TempFile, NewName, NewScore
12 Set Inserted = 1
13 End If
14 Write TempFile, Student, Score
15 End While
16 If Inserted == 0 Then
17 Write TempFile, NewName, NewScore
18 End If
19 While NOT EOF(GivenFile)
20 Read GivenFile, Student, Score
21 Write TempFile, Student, Score
22 End While
23 Close GivenFile, TempFile
24 Copy scratch onto grades
21. Using Arrays in File Maintenance
Ø
Some=mes
it
is
beber
to
load
a
file
into
arrays
in
the
computer’s
internal
memory.
◦
Works
if
the
file
is
small
enough
to
fit
into
available
memory
◦
Also
good
if
there
are
many
changes
to
be
made
to
the
file
◦
Internal
memory
works
fast
and
can
be
more
efficient
than
using
a
scratch
file
General
procedure:
1.
Open
the
given
file
for
Input (to
be
read
from)
2.
Read
file
records
into
parallel
arrays,
one
array
for
each
field
3.
Close
the
file
(so
that
it
can
later
be
opened
for
Output)
4.
Make
the
desired
modifica=ons
to
the
arrays
5.
Open
the
file
for
Output
(which
erases
all
the
original
data
in
this
file)
6.
Write
the
contents
of
the
arrays
(the
modified
data)
to
the
given
file
7.
Close
this
file.
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
22. PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Pseudocode for Using Arrays for
File Maintenance
1 Declare Student[100] As String
2 Declare Test1[100] As Integer
3 Declare Test2[100] As Integer
4 Declare Count As Integer
5 Open “grades” For Input As DataFile
6 Set Count = 0
7 While NOT EOF(DataFile)
8 Read DataFile, Student[Count], Test1[Count]
9 Set Count = Count + 1
10 End While
11 Close DataFile
12 Open “grades” For Output As DataFile
13 For (K = 0; K < Count; K++)
14 Write “Enter Test 2 score for “ + Student[K]
15 Input Test2[K]
16 Write DataFile, Student[K], Test1[K], Test2[K]
17 End For
18 Close DataFile
23. 10.3 Merging Sequen/al Files
Ø Open
the
two
given
files,
File1 and
File2,
for
Input.
Ø
Open
the
file
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
files
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
24. 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
25. Example: The Big Merger
A
company
wants
to
merge
two
payroll
files
(payroll1
and
payroll2)
into
a
single
file.
Suppose
that
each
record
in
these
files
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
file
is
0,
“0”,
0.0.
We
will
merge
these
two
files
into
a
new
file
called
payroll.
The
variables
are
declared
below
and
the
rest
of
the
program
is
on
the
next
slides
1 Declare Number1, Number2 As Integer
2 Declare Name1, Name2 As String
3 Declare Rate1, Rate2 As Float
Con=nued
on
next
slide ò
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
26. PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
The Big Merger (con/nued)
4 Open “payroll1” For Input As File1
5 Open “payroll2” For Input As File2
6 Open “payroll” For Output As File3
7 Read File1, Number1, Name1, Rate1
8 Read File2, Number2, Name2, Rate2
9 While (Number1 != 0) AND (Number2 != 0)
10 If Number1 < Number2 Then
11 Write File3, Number1, Name1, Rate1
12 Read File1, Number1, Name1, Rate1
13 Else
14 Write File3, Number2, Name2, Rate2
15 Read File2, Number2, Name2, Rate2
16 End If
17 End While
18 While Number1 != 0
19 Write File3, Number1, Name1, Rate1
20 Read File1, Number1, Name1, Rate1
21 End While
22 While Number2 != 0
23 Write File3, Number2, Name2, Rate2
24 Read File2, Number2, Name2, Rate2
25 End While
26 Write File3, 0, “0”, 0.0
27 Close File1, File2, File3 Con=nued
on
next
slide ò
27. The Big Merger (con/nued)
To
understand
this
program
beber,
walk
through
the
pseudocode
using
files
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
28. 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,
January’s
sales
are
subtotaled
and
printed.
◦ Then
the
program
starts
adding
up
February’s
sales.
◦ The
cycle
repeats
when
February
changes
to
March
and
so
on…
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE