wubwubnoobnoob wubwubnoobnoob - 2 months ago 11
C++ Question

Wrong output to a file, C++

I am trying to output some numbers to a file. Here's how:

std::ofstream out ("%path%/perm.out");
if (Pos == N)
{
for (i = 0; i < N; i++)
out << a[i] << " ";
out << "\n";
return;
}


I expect the result to look like this, since I'm trying to calculate permutations:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1


However, what I get is this:

3 2 1


But if I try to output this to console using std::cout, I get the result which I expected. Why does this happen and how do I fix this?

Answer

You need to open the file in Append mode.

std::ofstream myfile;
myfile.open ("example.txt", ios::app); 
Comments