std::ios_base::ate
(例如 std::ios_base::app
除外)和 std::的意义何在ios_base::trunc
(例如std::ios_base::out
除外)?
我应该更喜欢写 std::ios_base::smth
而不是 std::ios::smth
?
最佳答案
std::ios_base::ate 将光标定位在文本的末尾,而 std::ios_base_app 在末尾附加文本(带有写操作),尽管您仍然可以从头开始阅读:)
std::ios_base::trunc 截断文件,使其被清空,而 std::ios_base::out 只是指定您要写入流。
我目前无法引用该标准(在我的平板电脑上,Acrobat Reader 不允许复制),但从 ISO 14882:1998 的第 27.4.2.1.4 段中,您可以在链接上看到的信息几乎是准确的: http://cplusplus.com/reference/iostream/ios_base/openmode/
总结一下:
std::ios_base::app = append
通过“seek[ing] to end before each write”追加到流的末尾
std::ios_base::ate = At The End
打开后立即在末尾查找
std::ios_base::binary = binary
以二进制而不是文本执行操作
std::ios_base::in = input
以阅读模式打开
std::ios_base::out = output
以写入模式打开
std::ios_base::trunc = truncate
在打开时截断流。
这些值只是标志,因此您可以在末尾使用读/写二进制文件打开流:
std::ios_base::in | std::ios_base::out | std::ios_base::ate | std::ios_base::binary
关于使用这些值的方式,如你所愿。它们在 std::ios_base
类中被声明为公共(public)静态字段(参见 27.4.2),因此可以使用 std::ios::ate
甚至访问它们类似于 cout.binary
!
你必须注意的一点是 std::ios_base::ate
并不意味着 std::ios_base::app
也不意味着 std: :ios_base::out
隐含 std::ios_base::trunc
。每个字段都有不同的含义,也有不同的使用情况,虽然大部分都不能单独使用:)
关于c++ - std::ios_base::ate 和 std::ios_base::trunc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12253183/