write to file ... trivial?
Denis Koroskin
2korden at gmail.com
Thu Oct 7 01:06:01 PDT 2010
On Thu, 07 Oct 2010 01:36:23 +0400, Dr. Smith <iam at far.out> 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.
More information about the Digitalmars-d-learn
mailing list