I have noticed the foolowing details regarding fstream.

the following combination of openstate flags do not truncate the files.

ios_base::out | ios_base::in | <any flag other than ios_base::trunc>
ios_base::out | ios_base::app | <any flag other than ios_base::trunc>

the following combinations are resulting in a failbit

ios_base::app | aos_base::ate

and all other combinations open the file but truncate it.
is this standard behaviour? the reason i ask this is that i find it very wierd that the following combination truncates the file

ios_base::ate | ios_base::out

if this is standard behaviour, then there seems to be no way to use ate with out. i.e create an output stream which doesnt truncate without using app.

please confirm this. and suggest a workaround(if possible for the above problem).

4 Years
Discussion Span
Last Post by TTTHXC

well this is simple if you look at what each does.

ios_base::app - set file to append mode
ios_base::ate - set file to end.

therefore setting ios_base::ate | ios_base::app tell fstream to append to the end of the file. theoretically, this is impossilbe since the eof is set at the end of the file and therefore nothing can placed there, as then it would not be the eof.

as for ios_base::out | ios_base::ate this tells fstream to set to ouput mode and get data from the end of the file, where there is no data so the char stream would be null.

Edited by TTTHXC

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.