1 / 25

An Introduction to Programming with C++ Fifth Edition

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.

jricks
Download Presentation

An Introduction to Programming with C++ Fifth Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related