Hey. I am trying to output a file. I pretty much have down, but the only problem is I'm getting the last set of my numbers and missing the other 5000 or so sets.

Here is what I have

[

for (int i=0; i < N_plates; i++)
	{
		cout << i << "\t";
		cout << p[i][0] <<  "\t";
		cout << p[i][1] << "\t";
		cout << p[i][2] << endl;
		ofstream out("C:\\Users\\Yaser\\Documents\\myfile.txt");
		out << p[i][0] << "\t";
		out << p[i][1] << "\t";
		out << p[i][2] << "\n";

	}

Recommended Answers

All 3 Replies

Your problem is that you're recreating the file every time through the loop. Thus, the old data gets replaced.

To add to that - you generally don't want to open (and close) a file within a loop- that can actually slow the program down considerably.

You should open the file before the loop, do the writing, then close the file after the loop concludes.

If you absolutely, positively have to open the file within the loop, and you want what's written to be added to the previous content, you mus open it in append mode, as in:

ofstream out("C:\\Users\\Yaser\\Documents\\myfile.txt", ios::app);
commented: Why even bother? You're piling on useless information. -2

Thanks guys. Great input.

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.