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