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

落花浮王杯 提交于 2019-12-07 18:41:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!