Presentation is loading. Please wait.

Presentation is loading. Please wait.

Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 6: Sequential Data Files.

Similar presentations


Presentation on theme: "Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 6: Sequential Data Files."— Presentation transcript:

1 Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 6: Sequential Data Files

2 2 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

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

4 4 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.

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

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

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

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

10 10 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 GivenFile Open “scratch” for Output As TempFile Write “Enter name to be deleted: “ Input DeleteName While NOT EOF(GivenFile) Read GivenFile, Student, Score If Student <> DeleteName Then Write TempFile, Student, Score End If End While Close GivenFile, TempFile Continued….

11 11 Example: Modifying a Sequential 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 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.

12 12 6.3 Merging Sequential 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

13 13 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…

14 14 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

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


Download ppt "Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 6: Sequential Data Files."

Similar presentations


Ads by Google