I've found some similar posts on opening different files with the same file stream but they are difficult to understand. I'm trying to figure out for a larger project why I can't use the same file stream as in this example and what an alternative solution may be. And yes I am aware that this example opens the same file twice.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
		string line;ofstream thefile;


	thefile.open ("example.txt");
	thefile <<"writing this to a file. \n";
	thefile.close();


	ifstream myfile ("example.txt");
	if (myfile.is_open())
	{
		while (! myfile.eof())
		{
			getline (myfile, line);
			cout << line << endl;
		}
		//myfile.close();
	}
	else cout << "unable to open file";
	
	
	cout<<line;
	cin>>line;//used only for a makeshift pause here
	

	myfile.open ("example.txt");
	if (myfile.is_open())
	{
		while (! myfile.eof())
		{
			getline (myfile, line);
			cout << line << endl;
		}
		myfile.close();
	}
	else cout << "unable to open file";
	return 0;
}

I'm using microsoft visual c++ 2010 express
vista 64 bit operating system
this bit of code is portions taken from a C++ language tutorial by Juan Soulie and slightly modified for this example.

Any help that would help me get an idea of how this doesn't work and a solution that would help me open multiple and diffent files would be greatly appreciated.

Why did you comment out line 23? Seems like lines 32 and 33 are backwards. You should test if it's open, then if it's open, close it. And it never hurts to clear the bits too.

I'm sure this is overkill, but I imagine it'll work. Experiment around.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
		string line;ofstream thefile;


	thefile.open ("example.txt");
	thefile <<"writing this to a file. \n";
	thefile.close();


	ifstream myfile ("example.txt");
	if (myfile.is_open())
	{
		while (! myfile.eof())
		{
			getline (myfile, line);
			cout << line << endl;
		}
		myfile.close();
	}
	else cout << "unable to open file";
	
	
	cout<<line;
	cin>>line;//used only for a makeshift pause here
	

	if (myfile.is_open())
        {
            myfile.close();
        }

        myfile.clear();

	myfile.open ("example.txt");
	if (myfile.is_open())
	{
		while (! myfile.eof())
		{
			getline (myfile, line);
			cout << line << endl;
		}
		myfile.close();
	}
	else cout << "unable to open file";
	return 0;
}

You can never have too many "clear" statements, though often they're compeletely useless. Anyway, give it a shot. Experiment.

If a file fails to open or an error bit gets set (like EOF) then you have to clear them before opening a new one.

VernonDozier- yeah....oops I did comment out that line. I was experimenting with the code and didn't quite put it back. Thanks so much for the clear example and tips. I was able to use it to fix my larger project. The code was maybe a little overkill because I got it to work even deleting a few added lines but it's good practice and good to know more than one way of doing something.


pseudorandom21-Thanks for a little more technical perspective of what's going on.

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.