Chapter 6: Sequential Data Files

Slides:



Advertisements
Similar presentations
File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
Advertisements

An Introduction to Programming with C++ Fifth Edition Chapter 13 Sequential Access Files.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
Chapter 2: Developing a Program Extended and Concise Prelude to Programming Concepts and Design Copyright © 2003 Scott/Jones, Inc.. All rights reserved.
Chapter 1 Program Design
Chapter 6 AN INTRODUCTION TO FILES AND FILE PROCESSING Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Topics Introduction Hardware and Software How Computers Store Data
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Developing a Program.
Programming Logic and Design Seventh Edition
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 6: Sequential Data Files.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Chapter 11 File Systems and Directories. 2 File Systems File: A named collection of related data. File system: The logical view that an operating system.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
13-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Sequential Files Chapter 13. Master Files Set of files used to store companies data in areas like payroll, inventory Set of files used to store companies.
Now, please open your book to page 60, and let’s talk about chapter 9: How Data is Stored.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
Pascal Programming Today Chapter 11 1 Chapter 11.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
13-1 Sequential File Processing Chapter Chapter Contents Overview of Sequential File Processing Sequential File Updating - Creating a New Master.
13- 1 Chapter 13.  Overview of Sequential File Processing  Sequential File Updating - Creating a New Master File  Validity Checking in Update Procedures.
Master File Update Processing. Objectives On completing this section you should be able to: w Distinguish between online processing and batch processing.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 11: Sequential File Merging, Matching, and Updating Programming Logic and Design, Third Edition Comprehensive.
11 Chapter 111 Sequential File Merging, Matching, and Updating Programming Logic and Design, Second Edition, Comprehensive 11.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Chapter 1: Introduction to Computers and Programming
Chapter 14: Sequential Access Files
Topics Introduction Hardware and Software How Computers Store Data
Chapter 2 - Introduction to C Programming
Topics Introduction to Repetition Structures
Chapter 7 Text Input/Output Objectives
Ch. 8 File Structures Sequential files. Text files. Indexed files.
Starting Out with Programming Logic & Design
Chapter 2 - Introduction to C Programming
For Monday Read WebCT quiz 18.
Chapter 1: Introduction to Computers and Programming
Programming Logic and Design Eighth Edition
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Databases Lesson 2.
Programming Logic and Design Fourth Edition, Comprehensive
files Dr. Bhargavi Goswami Department of Computer Science
For Wednesday No new reading No quiz.
Data Groupings: File File: a group of related records
Chapter 3: Selection Structures: Making Decisions
Topics Introduction to File Input and Output
Chapter 9: More About Data, Arrays, and Files
Chapter 3: Selection Structures: Making Decisions
Topics Introduction to File Input and Output
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

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

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

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

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.

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

Open “grades” for Output As NewFile 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 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

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) …. End While Close GradeFile

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.

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.

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

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.

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

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…

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

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