简体   繁体   中英

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?

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.

iostream打开模式大致对应于C库中的fopen模式,而fopen具有截断的w模式和附加a模式,但不能将两者组合。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM