Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ifstream spits out error, but read is working ??

Status
Not open for further replies.

tkjordantk

Programmer
May 19, 2008
5
The following code works as expected. It reads data from the input file into the array and displays the array. The problem is that my error checking mechanism "if(!fin.good())" is executing and I'm not sure why...

The file I'm reading from is a simple plain text file (formatted data) with:
5 5.35
15 5.5
30 5.75

Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	// Variables where we will store the data read from the file
	double myArray[3][2];
	
	// Creates an ifstream object (fin) and tries to open the file data.dat
	ifstream fin("data.dat");
	
	// Checks to see if the file was opened correctly
	if(!fin)
	{
		cout << "Can't open file.\n";
		return 1;
	}
	// Reads the data into variables
	for(int i = 0; i < 3; i++)
	{
		for(int j = 0; j < 2; j++)
		{
			cout << "Reading value [" << i << "][" << j << "] - ";
			fin >> myArray[i][j];
			cout << myArray[i][j] << "\n";
		}
	}
	// Closes the file
	fin.close();
	
	// Make sure there were no problems
	if(!fin.good())
	{
		cout << "We have a problem.\n";
		return 1;
	}
	
	// Print the data to the screen
	for(int i = 0; i < 3; i++)
	{
		for(int j = 0; j < 2; j++)
		{
			cout << "Var[" << i << "][" << j << "] = " << myArray[i][j] << "\n";
		}
	}
	return 0;
}
 
You're calling fin.good() after you close the file. I'm pretty sure it's supposed to fail after you close the file.
 
Thanks, that makes sense to me.
The book I'm using as a reference has this setup in multiple examples, Herb Schildt's C++ Programming Cookbook.

 
Hi,I am interested to know if this prog. ran ok after you changed "fin.good ()",and what the correct line should have been?
I have H. Schildt's "C The Complete Reference" 1995,could there be errors in this as well?

Thanks,
Freddy.
 
You could call fin.is_open() instead of fin.good(), but I'm not sure how useful it would be to check if the file was closed properly.
 
After some further investigation, I've discovered this throughout the I/O section. The book says that in the following code, the first "!finout.good()" checks to make sure there were no errors when reading the file. The second one is to make sure there were no errors when the file is closed.

Code:
...
finout.get(ch);
if(!finout.good())
{
    cout << "error reading";
}

finout.close();

if(!finout.good())
{
    cout << "error closing";
}
 
I think the book is wrong.
According to Bjarne Stroustrup (The Creator of C++):
Stroustrup said:
If the state is good() the previous input operation succeeded. If the state is good(), the next input operation might succeed; otherwise it will fail.
So good() has nothing to do with whether the file closed properly; it only applies to whether the previous input operation succeeded.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top