Chapter 10 Sequential Data Files PRELUDE TO PROGRAMMING

  • Slides: 28
Download presentation
Chapter 10 Sequential Data Files PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Chapter 10 Sequential Data Files PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

10. 1 An Introduction to Files A file is a collection of information that

10. 1 An Introduction to Files A file is a collection of information 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, 6 TH EDITION BY ELIZABETH DRAKE

Classification of Files By content: • Text, readable by humans ◦ ASCII characters only

Classification 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: ◦ Sequential 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, 6 TH EDITION BY ELIZABETH DRAKE

Text Files or Binary Files? Binary files ◦ Used by most operating system files,

Text Files or Binary Files? Binary files ◦ Used by most operating system files, program files, and data files produced by applications Advantages of text files ◦ Easier to create by using a text editor ◦ Can be displayed on screen or printed without any special software ◦ Universal: virtually any computer system can interpret their contents without special software PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Records and Fields ØOne file may be broken up into groups of related data,

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, 6 TH EDITION BY ELIZABETH DRAKE

Records and Fields in a Data File PRELUDE TO PROGRAMMING, 6 TH EDITION BY

Records and Fields in a Data File PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Records and Fields in a Data File Ø The fields are separated from one

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, 6 TH EDITION BY ELIZABETH DRAKE

Sequential and Direct-Access Files Data files can be divided into two other categories: Sequential

Sequential and Direct-Access Files Data files can be divided into two other categories: Sequential 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 50 th record in a sequential 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 ◦ locating a data item is like finding a certain track on a DVD PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Creating a Sequential File Ø Open the file. Specify: ◦ External name: the full

Creating a Sequential 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) after the last record if the file was created in the program ◦ Closes the file if the file is an Input file PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Example: Opening and Closing a File Ø If we issue the command: Open “grades”

Example: Opening and Closing a File Ø If we issue the command: Open “grades” for Output As New. File ◦ grades is the external name ◦ New. File is the internal name ◦ The mode is Output Ø If we issue the command: Close New. File 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, 6 TH EDITION BY ELIZABETH DRAKE

Example: Reading a File Ø To open a file to be read: Open “grades“

Example: Reading a File Ø To open a file to be read: Open “grades“ for Input As Grade. File Ø Read the internal filename and the fields/variables: Read Grade. File, Name, Score Ø Read records within a loop (EOF() is discussed later): While NOT EOF(Grade. File) Read Grade. File, Name, Score. . . End While Close Grade. File PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Example: Creating a Sequential File 1 Declare Student As String 2 Declare Score As

Example: Creating a Sequential File 1 Declare Student As String 2 Declare Score As Integer 3 Open “grades” For Output As New. File 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 New. File, 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 New. File PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

The EOF() Function To terminate the input or output process of a loop that

The EOF() Function 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() function: EOF(Internal. Name) The EOF() function Ømay appear in the test condition of any loop or selection structure Øhas the value true if the end of the file Internal. Name 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() function is illustrated in the example in the next slide PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Example: Using the EOF() Function 1 2 3 4 5 6 7 8 Declare

Example: Using the EOF() Function 1 2 3 4 5 6 7 8 Declare Student As String Declare Score As Integer Open “grades” For Input As Grade. File While NOT EOF(Grade. File) Read Grade. File, Student, Score Write Student + “ “ + Score End While Close Grade. File PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

10. 2 Modifying a Sequential File Ø The most common operations on sequential files

10. 2 Modifying a Sequential File Ø The most common operations on sequential files are: ◦ deleting an existing record ◦ changing an existing record ◦ inserting or adding a new record Ø Read the file, one record at a time, rewriting each record to a temporary or scratch file until reaching the one to be modified. Ø The mode is Input for the original file and Output for the scratch file. Ø If modifying an existing record, make the change and write the new version to the scratch file. PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Modifying a Sequential File (continued) Ø If deleting an existing record, skip over the

Modifying a Sequential File (continued) Ø If deleting an existing record, skip over the record to be deleted. Ø If inserting a new record, read down to the proper location, then write the new record. Ø If the location 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, 6 TH EDITION BY ELIZABETH DRAKE

Example: Modifying a Sequential file Deleting a record from a file that contains student

Example: Modifying a Sequential file Deleting a record from a file that contains student names and one test score: 1 Declare Student As String 2 Declare Delete. Name As String 3 Declare Score As Integer 4 Open “grades” For Input As Given. File 5 Open “scratch” For Output As Temp. File 6 Write “Enter name of student to be deleted: ” 7 Input Delete. Name 8 While NOT EOF(Given. File) 9 Read Given. File, Student, Score 10 If Student != Delete. Name Then 11 12 Write Temp. File, Student, Score End If 13 End While 14 Close Given. File, Temp. File PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Example: Modifying a Sequential file (continued) The updated file on the previous slide is

Example: Modifying a Sequential file (continued) The updated file on the previous slide is now named Temp. File. 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 Target. File Open “scratch” for Input As Source. File While NOT EOF(Source. File) Read Source. File, Student, Score Write Target. File, Student, Score End While Close Source. File, Target. File Note: the scratch file still contains the information but it doesn’t matter. The next time you open the scratch file to modify another file, the old contents will be erased. PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Example: Modifying One Field in One Record 1 2 3 4 5 6 7

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 New. Score As Integer Open “grades” For Input As Given. File Open “scratch” For Output As Temp. File Write “Enter the name of the student: “ Input Name Write “Enter new test score: “ Input New. Score While NOT EOF(Given. File) Read Given. File, Student, Score If Student == Name Then Write Temp. File, Student, New. Score Else Write Temp. File, Student, Score End If End While Close Given. File, Temp. File Copy the file scratch onto the file grades PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Example: Inserting a Record into a Sequential File 1 2 3 4 5 6

Example: Inserting a Record into a Sequential 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 New. Name As String Declare New. Score As Integer Open “grades” For Input As Given. File Open “scratch” For Output As Temp. File Write “Enter name and score for new student: ” Input New. Name, New. Score Set Inserted = 0 While (NOT EOF(Given. File)) AND (Inserted == 0) Read Given. File, Student, Score If New. Name < Student Then Write Temp. File, New. Name, New. Score Set Inserted = 1 End If Write Temp. File, Student, Score End While If Inserted == 0 Then Write Temp. File, New. Name, New. Score End If While NOT EOF(Given. File) Read Given. File, Student, Score Write Temp. File, Student, Score End While Close Given. File, Temp. File Copy scratch onto grades PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Using Arrays in File Maintenance Ø Sometimes it is better to load a file

Using Arrays in File Maintenance Ø Sometimes it is better 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 modifications 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, 6 TH EDITION BY ELIZABETH DRAKE

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Declare Student[100] As String Declare Test 1[100] As Integer Pseudocode for Using Arrays for Declare Test 2[100] As Integer File Maintenance Declare Count As Integer Open “grades” For Input As Data. File Set Count = 0 While NOT EOF(Data. File) Read Data. File, Student[Count], Test 1[Count] Set Count = Count + 1 End While Close Data. File Open “grades” For Output As Data. File For (K = 0; K < Count; K++) Write “Enter Test 2 score for “ + Student[K] Input Test 2[K] Write Data. File, Student[K], Test 1[K], Test 2[K] End For Close Data. File PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

10. 3 Merging Sequential Files Ø Open the two given files, File 1 and

10. 3 Merging Sequential Files Ø Open the two given files, File 1 and File 2, for Input. Ø Open the file that will hold the merged records, File 3 for Output Ø Successively Read records from File 1 and File 2 Ø If the current record for File 1 precedes that of File 2, then write the File 1 record to File 3; otherwise, write the File 2 record to File 3 Ø Close three files PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

General Pseudocode for Merging Files Read the first record from each file While (NOT

General Pseudocode for Merging Files Read the first record from each file While (NOT EOF(File 1)) AND (NOT EOF(File 2)) Compare the current records for File 1 and File 2 If the File 1 record precedes the File 2 record Then Write the File 1 record to File 3 Read another record from File 1 Else Write the File 2 record to File 3 Read another record from File 2 End If End While Read the remaining records, if any, in File 1 and write them to File 3 Read the remaining records, if any, in File 2 and write them to File 3 PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

Example: The Big Merger A company wants to merge two payroll files (payroll 1

Example: The Big Merger A company wants to merge two payroll files (payroll 1 and payroll 2) 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 2 3 Declare Number 1, Number 2 As Integer Declare Name 1, Name 2 As String Declare Rate 1, Rate 2 As Float Continued on next slide PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE

4 5 6 7 8 9 10 11 12 13 14 15 16 17

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 “payroll 1” For Input As File 1 Open “payroll 2” For Input As File 2 Open “payroll” For Output As File 3 Read File 1, Number 1, Name 1, Rate 1 Read File 2, Number 2, Name 2, Rate 2 While (Number 1 != 0) AND (Number 2 != 0) If Number 1 < Number 2 Then Write File 3, Number 1, Name 1, Rate 1 Read File 1, Number 1, Name 1, Rate 1 Else Write File 3, Number 2, Name 2, Rate 2 Read File 2, Number 2, Name 2, Rate 2 End If End While Number 1 != 0 Write File 3, Number 1, Name 1, Rate 1 Read File 1, Number 1, Name 1, Rate 1 End While Number 2 != 0 Write File 3, Number 2, Name 2, Rate 2 Read File 2, Number 2, Name 2, Rate 2 End While Write File 3, 0, “ 0”, 0. 0 Close File 1, File 2, File 3 The Big Merger (continued) PRELUDE TO PROGRAMMING, 6 TH EDITION BY ELIZABETH DRAKE Continued on next slide

The Big Merger (continued) To understand this program better, walk through the pseudocode using

The Big Merger (continued) To understand this program better, walk through the pseudocode using files with the indicated records: payroll 1: payroll 2: 115, “Art”, 11. 50 120, “Dan”, 14. 00 130, “Ben”, 12. 25 125, “Eva”, 15. 50 135, “Cal”, 13. 75 0, “ 0”, 0. 0 Then, after 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, 6 TH EDITION BY ELIZABETH DRAKE

10. 4 Control Break Processing When we need subtotals (perhaps for a report), we

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, 6 TH EDITION BY ELIZABETH DRAKE