954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Opening multiple files with the same file stream

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.

seamus400
Newbie Poster
7 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

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.

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

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.

seamus400
Newbie Poster
7 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: