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";

8 Years
Discussion Span
Last Post by Yaserk88

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);
Votes + Comments
Why even bother? You're piling on useless information.
This question has already been answered. 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.