Home arrow C++ arrow Page 7 - Persistent Data: File Input and Output

Persistent Data: File Input and Output

Persistence is important, particularly to programmers. Data should be persistent as well; that is, it should survive when the program is finished. This article will show you how to make your data persistent by saving it to a file. It is excerpted from chapter 13 of the book C++ Demystified, written by Jeff Kent (McGraw-Hill, 2004; ISBN: 0072253703).

Author Info:
By: McGraw-Hill/Osborne
Rating: 4 stars4 stars4 stars4 stars4 stars / 18
August 25, 2005
  1. · Persistent Data: File Input and Output
  2. · First Argument—Specifying the File to Be Opened
  3. · Opening a File for Reading
  4. · Closing a File
  5. · Reading from a File
  6. · Looping Through the File
  7. · Summary

print this article

Persistent Data: File Input and Output - Summary
(Page 7 of 7 )

Data is persistent when it survives after the program is finished or even after the computer is turned off. Data stored in variables does not persist because RAM, where the variables are stored, is cleared when the program (or the computer) stops running. It is necessary to save data to a file on the computer’s hard drive or other storage medium so that data later can be retrieved when needed.

This chapter showed you how to make your data persistent by saving it to a file. Since saving the data accomplishes little unless you can later retrieve it, this chapter also showed you how to retrieve data from a file.

A file is a collection of data. It is located on persistent storage, such as a hard drive, CD-ROM, or other storage device.

Files store data in one of two formats, text and binary. Text files store data that has been converted into strings of ASCII characters. By contrast, binary files store data in the same format in which data is stored in RAM, fundamentally ones and zeroes. Notepad and other plain-text editors use text files. Binary files may store more complex data, and therefore are used in more complex programs, such as word processing, spreadsheet, or database programs.

You should include the fstream standard library when your program reads from, or writes to, files. This standard library defines three data types. The ofstream data type represents the output file stream, the direction of output being from your program out to a file. The ifstream data type represents the input file stream, the direction of input being from a file into your program. Finally, the fstream data type represents the file stream generally, and has the capabilities of both ofstream and ifstream in that it may both write information to files and read information from files.

The process of accessing a file, whether to read it, write to it, or both, goes through the following steps. First, the file first must be opened to establish a path of communication between the file and a stream object in your program—fstream, ofstream, or ifstream—used to access the file. Second, your program then reads from, or writes to, the file. Third, and finally, your program closes the file, using the close member function, to free system resources that are required to maintain the path of communication between the file and the stream object in your program, and also to avoid a “sharing” problem caused by trying in one part of your program to open a file that in another part of the program previously was opened but not closed.

You use either the open member function or a constructor to open a file. A constructor is a function that is automatically called when you attempt to create an instance of an object, such as an fstream, ofstream, or ifstream object. Either the open member function or a constructor may use two arguments. The first argument is the relative or absolute path to the file. The second argument, which may be optional, is one or more file mode flags, which define how the file should be opened, whether for input, output, appending, or something else.

You cannot assume that a file was successfully opened for reading or writing. You can use the fail member function to check if a file was successfully opened. You also can check to see if the file stream object used to open the file is NULL.

You write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen, except that you use an ofstream or fstream object instead of the cout object. Similarly, you read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard, except that you use an ifstream (or fstream) object instead of the cin object.

You read a line of a file using either the getline member function if you are working with C-strings or the getline function if you are working with the C++ string class. You use the fail member function to test for the end of the file as you read line by line through a file.

File stream objects may be passed as function arguments. They should be passed by reference rather than by value since the internal state of a file stream object may change with an open operation even if the contents of the file have not changed.

  1. What does it mean for data to be persistent?

  2. What is a file?

  3. What are the two formats in which files store data?

  4. What standard library should you include when your program reads from, or writes to, files?

  5. Which of the three objects, fstream, ifstream, or ofstream, may be used both for file input and file output?

  6. What are the two functions you can use to open a file?

  7. What is the purpose of opening a file?

  8. What is the purpose of closing a file?

  9. What is a constructor?

  10. Which is a better choice for detecting end of file in a text file, the eof member function or the fail member function?

  11. Should file stream objects be passed as function arguments by value or by reference?

  12. What does it mean for data to be persistent?


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials