On Thu, 07 Oct 2010 01:36:23 +0400, Dr. Smith <[email protected]> wrote:
Thank you. Indeed, I forgot: auto f = File("outfile.txt", "w");
Interestingly, this apparently works within a for-loop to overwrite the
file on
the first iteration and appending otherwise (Should there not be an
explicit
append arg?):
That's a correct behavior. See how it works: when you open a file, you a
get a "pointer" into that file. Every time you write N bytes to that file,
your pointer advances the same amount. E.g.
file.write("aaa");
file.write("bbb");
is essentially the same as:
file.write("aaabbb");
If that is not that you want, then you need to store you pointer before
writing, and seek back afterwards:
(I don't know the proper syntax so that is a pseudo-code)
auto pos = file.tell();
file.write("aaa");
file.seek(pos);
file.write("bbb");
In this case, you will first write "aaa" to file and then overwrite it
with "bbb".
That's the way file I/O works in every OS I know of and that's probably
because that's the way hard disks work in general.
Hope that helps.