Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::ios_base::ate and std::ios_base::trunc

Tags:

c++

What the point of the std::ios_base::ate (other than std::ios_base::app, for example) and std::ios_base::trunc (other than std::ios_base::out, for example)?

And should i preferly write std::ios_base::smth instead of std::ios::smth?

like image 486
FrozenHeart Avatar asked Sep 06 '25 07:09

FrozenHeart


1 Answers

std::ios_base::ate position the cursor at the end of the text whereas std::ios_base_app appends text (with a write operation) at the end, though you can still read from the beginning :)

std::ios_base::trunc truncates the file so it is emptied, whereas std::ios_base::out just specify you want to write to the stream.

I currently can't quote the standard (on my tablet and Acrobat Reader won't let met copy) but from paragraph 27.4.2.1.4 from ISO 14882:1998 the information you can see on the link is almost exact: http://cplusplus.com/reference/iostream/ios_base/openmode/

To sum up:

std::ios_base::app = append

Append at the end of the stream by "seek[ing] to end before each write"

std::ios_base::ate = At The End

Open and seek immediately at the end after opening

std::ios_base::binary = binary

Perform operation in binary as opposed to text

std::ios_base::in = input

Open in read mode

std::ios_base::out = output

Open in write mode

std::ios_base::trunc = truncate

Truncate the stream on opening.

These values are just flags, so you can open a stream in read/write binary at the end with :

std::ios_base::in | std::ios_base::out | std::ios_base::ate | std::ios_base::binary

Concerning the way of using those values, it is as you wish. They are declared as public static fields in std::ios_base class (see 27.4.2) thus it is possible to access them using std::ios::ate or even something like cout.binary !


The points where you must take attention is that std::ios_base::ate does NOT imply std::ios_base::app nor does std::ios_base::out implies std::ios_base::trunc. Each field has a different meaning, and a different case of use, though most of them can't be used alone :)

like image 89
Geoffroy Avatar answered Sep 09 '25 05:09

Geoffroy