An Introduction to Programming with C++ Fifth Edition Chapter 13 Sequential Access Files.

Slides:



Advertisements
Similar presentations
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Advertisements

File Handling Advanced Higher Programming. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a.
Files Organisation sequential files. Readings u Schneider Chapter 8 u Shelly Cashman to 9.14; to 9.11 u Meyer to 2-37; 1995.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions.
Chapter 9: Sequential Access Files and Printing
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Chapter 15.
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.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
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.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Chapter 8 Data File Basics.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 6: Sequential Data Files.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
5 1 Data Files CGI/Perl Programming By Diane Zak.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Learners Support Publications Working with Files.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 14: Sequential Access Files
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 6: Sequential Data Files
Fundamentals of Python: First Programs
Starting Out with Programming Logic & Design
An Introduction to Programming with C++ Fifth Edition
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Interactive I/O Input from keyboard Must prompt user User friendly
files Dr. Bhargavi Goswami Department of Computer Science
Data Groupings: File File: a group of related records
Topics Introduction to File Input and Output
CHAPTER 4 File Processing.
Topics Introduction to File Input and Output
Presentation transcript:

An Introduction to Programming with C++ Fifth Edition Chapter 13 Sequential Access Files

An Introduction to Programming with C++, Fifth Edition2 Objectives Open a sequential access file Determine whether a file was opened successfully Write data to a sequential access file Read data from a sequential access file

An Introduction to Programming with C++, Fifth Edition3 Objectives (continued) Test for the end of a sequential access file Close a sequential access file Read information from and write information to a sequential access file in.NET C++

An Introduction to Programming with C++, Fifth Edition4 Concept Lesson File Types Using Sequential Access Files Creating and Opening a Sequential Access File Determining whether a File was Opened Successfully

An Introduction to Programming with C++, Fifth Edition5 Concept Lesson (continued) Writing Information to a Sequential Access File Reading Information from a Sequential Access File Testing for the End of a Sequential Access File Closing a Sequential Access File

An Introduction to Programming with C++, Fifth Edition6 File Types A program can “read” from or “write” to a file –Files to which information is written are output files –Files that are read by the computer are input files Types of files in C++ –Sequential Information is accessed in consecutive order –Random Can be accessed in consecutive or in random order –Binary Information can be accessed by its byte location

An Introduction to Programming with C++, Fifth Edition7 Using Sequential Access Files A sequential access file is often called a text file

An Introduction to Programming with C++, Fifth Edition8 Using Sequential Access Files (continued)

An Introduction to Programming with C++, Fifth Edition9 Creating and Opening a Sequential Access File You must create the input and output file objects used in a program –#include ifstream and ofstream classes –using std::ifstream; and using std::ios;

Creating and Opening a Sequential Access File (continued) An Introduction to Programming with C++, Fifth Edition10

An Introduction to Programming with C++, Fifth Edition11 Creating and Opening a Sequential Access File (continued)

An Introduction to Programming with C++, Fifth Edition12 Creating and Opening a Sequential Access File (continued)

An Introduction to Programming with C++, Fifth Edition13 Determining whether a File was Opened Successfully open() may fail when attempting to open a file –E.g., it will not be able to create an output file when the path in fileName does not exist, or when the disk is full

Determining whether a File was Opened Successfully (continued) An Introduction to Programming with C++, Fifth Edition14 ! is the Not logical operator

An Introduction to Programming with C++, Fifth Edition15 Writing Information to a Sequential Access File Field: single item of information about a person, place, or thing –E.g., a name, a salary, a SSN, or a price Record: a collection of one or more related fields –Contains data about a specific person, place, or thing –The college you are attending keeps student records Examples of fields include your SSN, name, address, phone number, credits earned, and grades earned

An Introduction to Programming with C++, Fifth Edition16 Writing Information to a Sequential Access File (continued)

An Introduction to Programming with C++, Fifth Edition17 Writing Information to a Sequential Access File (continued) To verify if information was written correctly, open the (sequential access) file in a text editor –E.g., the text editor in Visual C++ or Notepad

An Introduction to Programming with C++, Fifth Edition18 Reading Information from a Sequential Access File Use >> to read char and numeric data from a file Use getline() to read string data from a sequential access file –The default delimiter character is the newline character (‘\n’)

An Introduction to Programming with C++, Fifth Edition19 Reading Information from a Sequential Access File (continued)

An Introduction to Programming with C++, Fifth Edition20 Testing for the End of a Sequential Access File A file pointer keeps track of the next character either to read from or write to a file –When a sequential access file is opened for input, the file pointer is positioned before the first character –As characters are read, the pointer is moved forward

Testing for the End of a Sequential Access File (continued) An Introduction to Programming with C++, Fifth Edition21

An Introduction to Programming with C++, Fifth Edition22 Closing a Sequential Access File To prevent the loss of data, close a sequential access file as soon as program finishes using it

An Introduction to Programming with C++, Fifth Edition23 Summary Sequential access files can be input or output files To use a text file, program must contain: –include directive –using std::ios; statement Use the ifstream and ofstream classes to create input and output file objects, respectively Use is_open() to determine whether a text file was opened successfully

An Introduction to Programming with C++, Fifth Edition24 Summary (continued) Records in a text file are usually written on a separate line in the file –Use endl eof() determines if file pointer is at end of the file Use close() to close a file –Failing to close an open file can result in loss of data

An Introduction to Programming with C++, Fifth Edition25 Application Lesson: Using a Sequential Access File in a C++ Program Lab 13.1: Stop and Analyze Lab 13.2 –Program should allow flower shop owner to save in a text file each salesperson’s name and sales amount Also, display the total of the sales amounts in file Lab 13.3 –Modified program will allow the user to display contents of sales.txt file Lab 13.4: Desk-Check Lab Lab 13.5: Debugging Lab