250 likes | 266 Views
This chapter provides an introduction to programming with sequential access files in C++. Learn how to open, write to, read from, test for the end, and close a sequential access file.
E N D
An Introduction to Programming with C++Fifth Edition Chapter 13 Sequential Access Files
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 Edition
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 Edition
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 Edition
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 Edition
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 Edition
Using Sequential Access Files • A sequential access file is often called a text file An Introduction to Programming with C++, Fifth Edition
Using Sequential Access Files (continued) An Introduction to Programming with C++, Fifth Edition
Creating and Opening a Sequential Access File • You must create the input and output file objects used in a program • #include <fstream> • ifstream and ofstream classes • using std::ifstream; and using std::ios; An Introduction to Programming with C++, Fifth Edition
Creating and Opening a Sequential Access File (continued) An Introduction to Programming with C++, Fifth Edition
Creating and Opening a Sequential Access File (continued) An Introduction to Programming with C++, Fifth Edition
Creating and Opening a Sequential Access File (continued) An Introduction to Programming with C++, Fifth Edition
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 An Introduction to Programming with C++, Fifth Edition
Determining whether a File was Opened Successfully (continued) ! is the Not logical operator An Introduction to Programming with C++, Fifth Edition
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 Edition
Writing Information to a Sequential Access File (continued) An Introduction to Programming with C++, Fifth Edition
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 Edition
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 Edition
Reading Information from a Sequential Access File (continued) An Introduction to Programming with C++, Fifth Edition
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 An Introduction to Programming with C++, Fifth Edition
Testing for the End of a Sequential Access File (continued) An Introduction to Programming with C++, Fifth Edition
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 Edition
Summary • Sequential access files can be input or output files • To use a text file, program must contain: • include <fstream> 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 Edition
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 Edition
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 An Introduction to Programming with C++, Fifth Edition