std::ios_base::ate
(例如std::ios_base::app
以外)和std::ios_base::trunc
(例如std::ios_base::out
以外)的要點是什麼?std :: ios_base :: ate和std :: ios_base :: trunc
而且我應該更喜歡寫std::ios_base::smth
而不是std::ios::smth
?
std::ios_base::ate
(例如std::ios_base::app
以外)和std::ios_base::trunc
(例如std::ios_base::out
以外)的要點是什麼?std :: ios_base :: ate和std :: ios_base :: trunc
而且我應該更喜歡寫std::ios_base::smth
而不是std::ios::smth
?
的std ::的ios_base ::吃在年底光標在文本的結束位置,而標準:: 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
追加在流的由端
std::ios_base::ate = At The End
打開 「尋求[和]每個寫入之前結束」,並尋求IMMED在二進制iately在開口之後結束
std::ios_base::binary = binary
執行操作,而不是文本
std::ios_base::in = input
開啓在讀模式
std::ios_base::out = output
開啓在寫模式
std::ios_base::trunc = truncate
打開時截斷流ING。
這些值都只是標誌,這樣你就可以在打開讀取流/結尾與寫入二進制:
std::ios_base::in | std::ios_base::out | std::ios_base::ate | std::ios_base::binary
關於使用這些值的方式,如你所願。它們在std::ios_base
類中被聲明爲公共靜態字段(參見27.4.2),因此可以使用std::ios::ate
或甚至類似cout.binary
來訪問它們!
,你必須注意的事項是,std::ios_base::ate
並不意味着std::ios_base::app
也不std::ios_base::out
意味着std::ios_base::trunc
。每個字段有不同的含義和不同的使用情況,儘管其中大多數不能單獨使用:)
但std :: ios_base :: out也清除文件中的數據,不是?你能給我一個例子,其中std :: ios_base :: out不清除文件? – FrozenHeart
從這裏:http:// cplusplus。com/reference/iostream/ios_base/openmode /你得到每個值的定義。此外,這個答案@ veer的關於std :: ios_base :: in和std :: ios ::的評論在 – Geoffroy
中你是什麼意思?「你仍然可以從頭閱讀」?你能舉個例子嗎? – FrozenHeart
'std :: ios_base :: in'和'std :: ios :: in'應該是相同,這取決於你。 – oldrinb
@veer是他們,以及任何:: ios_base :: descendant :: in :) – Geoffroy