问题
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?
回答1:
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.
回答2:
The allowable combinations of flags are specified in [filebuf.members] in the standard. Table 132 gives the possibilities:

So, since the combination of trunc
and app
isn't in the table, the open is required to fail.
来源:https://stackoverflow.com/questions/26900392/why-does-fstream-open-fail-if-the-mode-has-both-trunc-and-app-set