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