c裡面的fflush函數

來源:互聯網
上載者:User

標籤:

NAME
  fflush - flush a stream

SYNOPSIS
  #include <stdio.h>

  int fflush(FILE *stream);

DESCRIPTION
  For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream‘s underlying write function.

      For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application. The open status of the stream is unaffected.

  如果給出的檔案流是一個輸出資料流,那麼fflush()把輸出到緩衝區的內容寫入檔案. 如果給出的檔案流是輸入類型的,那麼fflush()會清除輸入緩衝區。

  fflush()在調試時很實用,特別是對於在程式中輸出到螢幕前發生錯誤片段時. 直接調用 fflush( STDOUT )輸出可以保證你的調試輸出可以在正確的時間輸出. 

  If the stream argument is NULL, fflush() flushes all open output streams.

For a nonlocking counterpart, see unlocked_stdio(3).

1.

flush(stdin)重新整理標準輸入緩衝區,把輸入緩衝區裡的東西丟棄
fflush(stdout)重新整理標準輸出緩衝區,把輸出緩衝區裡的東西列印到標準輸出裝置上。

2.

fflush的真正作用就是立即將緩衝區中的內容輸出到裝置。正因為這樣,所以只能在寫入檔案的時候使用fflush。在讀取檔案時使用fflush是不好的編程方法,因為那樣的代碼在一些環境下可能正常工作,但在另一些環境下則會出錯,這導致程式不可移植。

 

flush即清空緩衝,在慢速裝置上使用緩衝是一種提升資料處理效率的手段,flush的作用是將記憶體中緩衝的內容實際寫入外存媒介
詳見MSDN的Kernel32!FlushFileBuffers

fclose後未必會flush一次的,作業系統會在CPU閒置時候執行flush

 

3.

fflush不應該在讀取檔案的時候用,而應該在寫入檔案的時候用。
fflush會清空緩衝區,fclose在關閉檔案之前也會清空緩衝區。如果使用exit函數退出程式,或者main函數返回,則所有開啟後沒有關閉的檔案會自動關閉,關閉時也會清空緩衝區。

通常,只有在程式非正常結束的情況下,緩衝區才不會被清除。


參考

fflush 函數    http://blog.chinaunix.net/uid-21651805-id-3482290.html

fflush函數有什麼作用?     http://blog.csdn.net/stpeace/article/details/9063293

 

c裡面的fflush函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.