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).
Persistent Data: File Input and Output - Closing a File (Page 4 of 7 )
Of course, you are not going to close a file as soon as you open it. You will read or write to the file first. However, closing a file is relatively simple, so I will discuss this issue out of order before discussing the more complex subjects of writing to, and reading from, a file.
You should close a file when you are finished reading or writing to it. While the file object will be closed when the program ends, your program’s performance will be improved if you close a file when you are finished with it because each open file requires system resources. Additionally, some operating systems limit the number of open “handles” to files. Finally, you will 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 close a file using, naturally enough, the close member function, which takes no arguments. The following example closes a file opened for writing:
ofstream outfile; outfile.open("students.dat"); // do something outfile.close();
The same syntax applies to closing a file for reading.
ifstream infile; infile.open("students.dat"); // do something infile.close();
Writing to a File
You output or 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. The only difference is that you use an ofstream or fstream object instead of the cout object.
You output or write information to a file from your program using the operator just as you use that operator to output information to the screen. The only difference is that you use an or object instead of the object.
The following program writes information inputted by the user to a file named students.dat, which is created if it does not already exist.
#include <fstream> #include <iostream> using namespace std; int main () { char data[80]; ofstream outfile; outfile.open("students.dat"); cout << "Writing to the file" << endl; cout << "===================" << endl; cout << "Enter class name: "; cin.getline(data, 80); outfile << data << endl; cout << "Enter number of students: "; cin >> data; cin.ignore(); outfile << data << endl; outfile.close(); return 0; }
The input and output could be
Writing to the file =================== Enter class name: Programming Demystified Enter number of students: 32
Open the file in a plain-text editor such as Notepad. The contents with the preceding sample input would be as follows:
Programming Demystified 32
The statement that wrote to the file included the endl keyword:
outfile << data << endl;
The reason is to write the name of the class (“Programming Demystified”) to a different line than the number of students, 32. Otherwise, the file contents would be
Programming Demystified32
NOTE: The call to the ignore member function after cin >> data follows the advice in Chapter 12 to clear the newline character from the input buffer after using the cin object with the stream extraction operator (>>).
NOTE: The call to the ignore member function after cin >> data follows the advice in Chapter 12 to clear the newline character from the input buffer after using the cin object with the stream extraction operator (>>).
You instead could have used an fstream object to write to the file. You would have changed the data type of outfile from ofstream to fstream and then changed the call to the open method to include two arguments:
Alternatively, you could have used the fstream constructor:
fstream outfile ("students.dat", ios::out);
If you want to append, you only need to add an ios:app flag to the second argument of the constructor or the open member function using the bitwise or operator (|).