Home arrow C++ arrow Page 5 - 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 - Reading from a File
(Page 5 of 7 )

You input or 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. The only difference is that you use an ifstream (or fstream) object instead of the cin object.

You input or read information from a file into your program using the operator just as you use that operator to input information from the keyboard. The only difference is that you use an (or ) object instead of the object.

The following program builds on the previous one. After writing information inputted by the user to a file named students.dat, the program reads information from the file and outputs it onto the screen:

#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();
ifstream infile;
cout << "Reading from the file" << endl;
cout << "=====================" << endl;
infile.open("students.dat");
infile >> data;
cout << data << endl;
infile >> data;
cout << data << endl;
infile.close();
return 0;
}

Sample input and output:

Writing to the file
===================
Enter class name: Programming
Enter number of students: 32
Reading from the file
=====================
Programming
32

Reading a Line of a File

With the same program, try entering a class name with an embedded space. The following is some sample input and output:

Writing to the file
===================
Enter class name: Programming Demystified
Enter number of students: 32
Reading from the file
=====================
Programming
Demystified

The following are the contents of the file after the inputted data was written to it:

Programming Demystified
32

The first read of the file did not read the first line of the file, “Programming Demystified.” Instead, the first read of the file only read the word “Programming” and then stopped. Consequently the second line of the program read the remainder of the first line of the file, “Demystified,” instead of the number of students.

The ifstream object together with the stream extraction operator reads the file sequentially, starting with the first byte of the file. The first attempt to read the file starts at the beginning of the file and goes to the first whitespace character (a space, tab, or new line) or the end of the file, whichever comes first. The second attempt starts at the first printable character after that whitespace, and continues to the next whitespace character or the end of the file, whichever comes first.

The first read attempt only read “Programming,” not “Programming Demystified,” because the read stopped at the whitespace between “Programming” and “Demystified.” The second attempt read “Demystified.” There were no further read attempts, so the number of students, 32, was never read.

This should seem like déjà vu. We encountered a similar issue in Chapter 10 using the cin object with the stream extraction operator (>>). As in Chapter 10 with the cin object, the solution is to use getline.

If you are working with C-strings, then you should use the getline member function. The only difference between using the getline member function here and in Chapter 10 is that here the getline member function is called by an ifstream or fstream object instead of a cin object. Accordingly, we need to replace the two calls to infile >> data with the following:

infile.getline(data, 80);

You also can use getline with the C++ string class. The only difference between using the getline member function here and in Chapter 10 is that here the first argument of the getline member function is an ifstream or fstream object instead of a cin object. Accordingly, we need to replace the two calls to infile >> data with the following:

getline(infile, data);

The following modification of the previous program uses the getline function with the C++ string class:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string data;
ofstream outfile;
outfile.open("students.dat");
cout << "Writing to the file" << endl;
cout << "===================" << endl;
cout << "Enter class name: ";
getline(cin, data);
outfile << data<< endl;
cout << "Enter number of students: ";
cin >> data;
cin.ignore();
outfile << data<< endl;
outfile.close();
ifstream infile;
cout << "Reading from the file" << endl;
cout << "=====================" << endl;
infile.open("students.dat");
getline(infile, data);
cout << data << endl;
getline(infile, data);
cout << data << endl;
infile.close();
return 0;
}

As the following sample input and output reflects, the first read now reads the entire first line of the file even when that line contains embedded spaces:

Writing to the file
===================
Enter class name: Programming Demystified
Enter number of students: 32
Reading from the file
=====================
Programming Demystified
32


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