qtreez
qtreez

Reputation: 121

Why is an fstream truncated when it is opened with the flags ios::ate and ios::out?

I opened my fstream with ios::ate and ios::out flags and I noticed that file is truncated. This problem does not occur if I also set a flag ios::in. Then output and input position are fine. But my questions is what is sense of ios::ate flag if file is truncated default in case when ios::in flag isn't set?

Upvotes: 4

Views: 1436

Answers (1)

KarlM
KarlM

Reputation: 1662

Despite the implication in the standard, ios::ate doesn't actually have anything to do with whether or not truncation happens.

Internally, the fstream object has a buffer (a filebuf object). Whether the contents of the buffer are appended to the file when you write, are purely dependent on the combination of ios::in and ios::out - you need both to append. (Or ios::app)

ios::ate doesn't append the buffer - it helps you calculate file size and relative positions within the file. Different things.

See this question for discussion of the various modes - you will note ios::ate has no effect on the mode, and that you need in|out (or app) to get +.

Upvotes: 4

Related Questions