Good evening ladies and gents,

Had a question concerning this piece of code that I'm trying out from a book, it supposed to open a file, write text to it, close it, reopen it, append text to it and then write the text from the file to the console screen, it works fine up untill the text has to be written to the console screen, then I get the message that the file can't be opened for reading though the text is added to the file?

What am I doing wrong?

#include <fstream>
#include <iostream>

using namespace std;

int main()	// returns 1 on error
{
	char fileName[80];
	char buffer[255]; // for user input.
	cout << "Please reenter the file name: ";
	cin >> fileName;

	ifstream fin(fileName);

	if (fin) // allready exists ?
	{
		cout << "Current file contents:\n";
		char ch;

		while (fin.get(ch))
			cout << ch;
		cout << "\n***End of file contents.***\n";
	}
	fin.close();

	cout << "\nOpening " << fileName << " in append mode...\n";

	ofstream fout(fileName, ios::app);
	if (!fout)
	{
		cout << "Unable to open " << fileName << " for appending !\n";
		return 1;
	}

	cout << "\nEnter text for this file: ";
	cin.ignore(1, '\n');
	cin.getline(buffer, 255);
	fout << buffer << "\n";
	fout.close();

	fin.open(fileName); // reassign existing fin object.
	if (!fin) // <-- problem is here.
	{
		cout << "Unable to open " << fileName << " for reading !\n";
		return 1;
	}

	cout << "\nHere's the contents of the file: \n";
	char ch;
	while (fin.get(ch))
		cout << ch;
	cout << "\n***End of file contents.***\n";
	fin.close();

	return 0;
}

Well, found a solution to solve the problem:

if (fin) // allready exists ?
	{
		cout << "Current file contents:\n";
		char ch;

		while (fin.get(ch))
			cout << ch;
		cout << "\n***End of file contents.***\n";
	}
	fin.close();
	fin.clear();

.......

The problem seems to be this:

fin returns 0 because "the base class iso (from which istream is inherited) provides an overloaded cast operator that converts a stream into a pointer of type void*. The value of the pointer is 0 if an error occurred while attempting to read a value or the end-of-file indicator was encoutered." (1)

So, If I understand this correctly, your input because of being casted to a type void pointer get's another address and therefore isn't recognised?

If so, why does the function clear() solve this?

When you read the file completely through the first time, the EOF flag was set in the object fin. That flag stays set until you use the fin.clear( ) statement. The act of closing, or reopening, the file does not clear the flag.

This does not really have anything to do with casting of the stream operator. However, that casting to a 0 upon error or end of file, non-zero otherwise, is handy for loop control when reading or for testing successful opening of files, as your code uses.

Ok, thanks for the explanation vmanes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.