This should work, but maybe just not exactly as you expect.
See, whenever you open a file for writing, it will remove the old file and start a new file. To fix this, pass "ios::out | ios::append" as...