Chapter 6 Sequential Data Files Extended Prelude to

  • Slides: 15
Download presentation
Chapter 6: Sequential Data Files Extended Prelude to Programming Concepts & Design, 3/e by

Chapter 6: Sequential Data Files Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

6. 1 An Introduction to Files • A file is a collection of information

6. 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. • Two types of files: – Text files – Binary files 6 -2

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

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 6 -3

More on Data Files • One file may be broken up into groups of

More on Data Files • 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: that customer’s name, address, and phone number. 6 -4

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 6 -5

Example • If we issue the command: Open “grades” for Output As New. File

Example • 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 6 -6

Reading a File • To open a file to be read: Open “grades“ for

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: While NOT EOF(Grade. File) Read Grade. File, Name, Score …. End While Close Grade. File 6 -7

6. 2 Modifying A Sequential File • The most common operations on sequential files

6. 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. 6 -8

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. 6 -9

Example: Modifying a Sequential File (continued) Deleting a record from a file that contains

Example: Modifying a Sequential File (continued) Deleting a record from a file that contains student names and scores on one test: Open “grades” for Input As Given. File Open “scratch” for Output As Temp. File Write “Enter name to be deleted: “ Input Delete. Name While NOT EOF(Given. File) Read Given. File, Student, Score If Student <> Delete. Name Then Write Temp. File, Student, Score End If End While Close Given. File, Temp. File Continued…. 6 -10

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 that 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. 6 -11

6. 3 Merging Sequential Files • Open the two given files, File 1 and

6. 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 6 -12

Problem Solving • When we need subtotals for a report, we use a technique

Problem Solving • When we need subtotals 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… 6 -13

Steps • Identify the input and output variables • Study the report that must

Steps • Identify the input and output variables • Study the report that must be produced to discern the calculations that must be done, and where breaks must be set – In the problem given in the text, breaks will occur when the store number changes • • Divide the tasks into modules Create the hierarchy chart Code the modules Test the modules 6 -14

Pseudocode Language (Ch 6) File I/O: Open “file” For Input/Output As Name Close Name

Pseudocode Language (Ch 6) File I/O: Open “file” For Input/Output As Name Close Name Read Name, Variable Write Name, Variable EOF(Name) 6 -15