LazerSharks
LazerSharks

Reputation: 3155

Why does fstream.open() fail "If the mode has both trunc and app set"?

It took me quite a while to figure out that my .open() call wasn't opening a file because I had both the trunc and app mode options set. I only figured this out after catching a little note written on the C++ docs.

This seems like a weird gotcha. Why is this the case? Can you not truncate the file and then append only? Or is this considered superfluous specification?

Upvotes: 3

Views: 649

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490108

The allowable combinations of flags are specified in [filebuf.members] in the standard. Table 132 gives the possibilities:

enter image description here

So, since the combination of trunc and app isn't in the table, the open is required to fail.

Upvotes: 4

Jonathan Wakely
Jonathan Wakely

Reputation: 171263

The iostream open modes correspond roughly to the fopen mode in the C library and fopen has a w mode that truncates and an a mode that appends, but no combination of the two.

Upvotes: 2

Related Questions