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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

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:

fstream outfile;
outfile.open("students.dat", ios::out);

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 (|).


blog comments powered by Disqus
C++ ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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