Re: [問題] 讀取變動的檔案
※ 引述《keterwang (哪裡有彩虹..)》之銘言:
: 最近在做一個有關sensor的東西
: 需要將一個程式輸出檔案(.DAT)最後一行拿出來做處理
: 也就是抓取最後寫進去的資料
: 因為這個DAT檔案是一直在寫入的狀態
: 請問一下
: 那我要讀最後一行的話是不是必須一直重複進行關檔讀檔的動作
: 來將更新的檔案再次存進Buffer 再來取出最後一行呢?
: 還是有更有效率的辦法可以更新Buffer並且將pointer指向最後一行呢?
: 因為那個程式是寫死的 所以沒辦法改變他的輸出方式orz
: 只能夠對那個DAT檔案下手
: 拜託各位了~!! 大感謝!
man fseek
The file positions reported by fgetpos(), ftell(), ftello() may be incorrect
for files that are open in text mode in the case that the file is being read,
or the case where the file is being written to and there is buffered data
which has not yet been written to the underlying file. In the case that the
file is being written to, fflush() can be used prior to calling fgetpos()
ftell(), or ftello() to ensure the accuracy of these functions. Text mode
files with no buffering do not have this problem, however performance can be
significantly degraded with buffering turned off.
看來是,你先記住現在的位置(就是你讀到目前的最後一行)
然後呼叫一次fflush
然後應該就可以繼續讀了...
除非你要跳跳跳,不然這樣應該可以一直讀下去不用用到fseek
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.123.102.167
推
08/26 08:08, , 1F
08/26 08:08, 1F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章