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();
	
 }

Recommended Answers

All 4 Replies

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)

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

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

Yes, that would work.

Thanks a lot. It worked.

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.