2013-08-04 28 views

回答

3

是的,std::ios_base::out相當於"w"fopen

std::ios_base::trunc這一點是當std::ios_base::instd::ios_base::out在同一時間使用。

  • in | out相當於"r+"
  • in | out | trunc相當於"w+"
  • binary | in | out等同於"rb+"
  • binary | in | out | trunc等同於"wb+"

也許表會更明顯:

binary in out trunc | stdio equivalent 
-----------------------+----------------- 
     + +   |  "r+" 
     + +  + |  "w+" 
    +  + +   |  "r+b" 
    +  + +  + |  "w+b" 
+0

我不明白你對std :: ios_base :: binary的正確理解。你什麼意思?我應該明確指定std :: ios_base :: trunc與std :: ios_base :: binary嗎? – FrozenHeart

+0

@FrozenHeart編輯。 'trunc'實質上是指「我想先寫」。 – timothyqiu

+0

好的,非常感謝 – FrozenHeart

1

這是多餘的 - 換句話說,如果你有或沒有沒有區別。

顯然,在一些組合,如std::ios_base::out | std::ios_base::in它不會是多餘的。

3

要替換文件的內容,而不是擴展文件,則必須在

std::ios_base::out | std::ios_base::trunc

打開輸出文件流開模出相當於out|trunc,就是trunc標誌可以被省略。

但對於雙向文件流,必須始終明確指定trunc

要擴展輸出文件,請使用標誌std::ios_base::ate | std::ios_base::app

這裏,文件內容被保留,因爲trunc標誌未設置,初始文件位置在文件末尾。

然而,另外trunc標記可以被設置,並且該文件的內容將被丟棄,並且該輸出在一個空文件的末尾完成。

相關問題