你在這裏依賴於未定義的行爲 - 沒有任何保證,如果你不使用std :: endl它不會刷新。不要依賴未定義或假定的行爲 - 只依賴明確指定的行爲。
要闡述:
1.3.24 [defns.undefined]
undefined behavior
behavior for which this International Standard imposes no requirements
[ Note: Undefined behavior may be expected when this International Standard omits any explicit definition of
behavior or when a program uses an erroneous construct or erroneous data. Permissible undefined behavior
ranges from ignoring the situation completely with unpredictable results, to behaving during translation or
program execution in a documented manner characteristic of the environment (with or without the issuance of
a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).
Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed.
— end note ]
C++ 11或C++ 03規定爲user2585330是依靠行爲沒有任何要求。
據我的描述可以看出,行爲與流緩存數據一致,如你期望的那樣,並且在刷新緩衝區之前立即尋找文件末尾。 –
這超出了該語言的範圍。當我們談論刷新一個文件時,我們正在沖洗它到OS/FileSystem來處理。這些事情如何處理交互過程將取決於所使用的操作系統和文件系統。 –