0

I have an assignment which will pull data from Trans.data and Lawn.data. Can I not use fin to open both the files at once or do i need to setup another stream. Here is my code I am getting the Error message I have set if Trans.data fails to open.

int main()
{
	//set ofstream and fstream to fin and fout
	ifstream fin;
	ofstream fout;

	//open input streams and output stream
	fin.open ("Lawn.data");
	if (fin.fail())
	{
		cout << "Error opening Lawn.data";
		exit(1);
	}
	fout.open ("Report.out");
	if (fout.fail())
	{
		cout << "Error opening Report.out";
		exit(1);
	}
	ProcessLawnData(fin);
	fin.close();
	fout.close();
	
 }
2
Contributors
4
Replies
5
Views
12 Years
Discussion Span
Last Post by lsu420luv
0

Do you mean have 2 different files simultaneously attached to one stream? No, that couldn't possibly work - how would your program know which file to read?

You can, of course close one file, then reuse the same stream to open a different file - but if you need them both at the same time, I'd suggest a second stream object (constantly opening and closing files isn't very efficient)

0

so how would I set second stream? would i just do another ofstream then the new name
like ifstream fin2?

0

so how would I set second stream? would i just do another ofstream then the new name
like ifstream fin2?

Yes, that would work.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.