清除读取流缓冲区 [英] clearing read stream buffers

查看:132
本文介绍了清除读取流缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道标准(或支持Linux,Mac,Win32)的方式

清除读取流缓冲区(标准ANSI C文件流)?


我甚至会选择平台特定的方式。


不,我知道我可以使用直接低级I / O或非缓冲来做

读取,但对于我的应用程序,我需要缓冲。我可以实现自己,

但这不是最优的。


例如,我使用fopen()打开一个只读文件。我定期从外部方法知道

,底层文件已经更新。

在我下一次读取流之前,我想要刷新读取

缓冲并强制它从磁盘(或内核缓存)读取。


fflush似乎适用于Linux,但不确定它是否标准。


我没有看到任何这样做的功能,但似乎非常需要。


我也可以通过fseeking到a触发刷新缓冲区很多

文件的不同区域。是否有任何fseek,即使是1指数offseting

也总是强迫缓冲区?

Does anyone know a standard (or supported on Linux, Mac, Win32) way to
clear a read stream buffer (standard ANSI C file stream)?

I would even settle for a platform specific way of doing it.

And no, I know I can use direct low level I/O or non-buffered to do
reads, but for my app, I need the buffering. I can implement myself,
but this is not optimal.

Example, I open a read only file using fopen(). I periodically know
from external methods, that the underlying file has been updated.
Before I do my next read on the stream, I want to flush the read
buffers and force it to read from disk (or kernel cache).

fflush seems to work on Linux, but not sure it is standard.

I don''t see any function to do this, but it seems very needed.

I can also trigger a flush of the buffers by fseeking to a much
different area of the file. Does any fseek, even offseting by 1 index
always force a flusshing of the buffers?

推荐答案



JG写道:

JG wrote:
有没有人知道标准(或支持Linux,Mac,Win32)方式
清除读取流缓冲区(标准ANSI C文件)流))

我甚至会选择平台特定的方式来实现它。

不,我知道我可以使用直接低级I / O或非缓冲做
读取,但对于我的应用程序,我需要缓冲。我可以实现自己,
但这不是最佳的。
Does anyone know a standard (or supported on Linux, Mac, Win32) way to clear a read stream buffer (standard ANSI C file stream)?

I would even settle for a platform specific way of doing it.

And no, I know I can use direct low level I/O or non-buffered to do
reads, but for my app, I need the buffering. I can implement myself,
but this is not optimal.




一旦你确定还有一堆角色

坐在输入流中(至少应该包含''\ n''),然后你

可以使用:


while(( c = fgetc(FOOFILE)!=''\ n'')

;



once you''ve determined that there is still a bunch of characters
sitting in the input stream (should include ''\n'' at least), then you
can use:

while( (c = fgetc(FOOFILE) != ''\n'' )
;


Luke Wu写道:
Luke Wu wrote:
一旦你确定还有一堆字符
坐在输入流中(至少应该包含''\ n''),那么你
可以使用:

while((c = fgetc(FOOFILE)!=''\ n'')
;
once you''ve determined that there is still a bunch of characters
sitting in the input stream (should include ''\n'' at least), then you
can use:

while( (c = fgetc(FOOFILE) != ''\n'' )
;




他没有问过典型的我如何忘记stdin中的问题

问题?


他问的是磁盘上的实际文件(不是管道/终端/等)他执行阅读,他怎么可以便携和有效地强制执行

打开读取流以从磁盘重新读取他想要的数据。问题是

来自另一个线程/流/ fd /进程的写入改变了

底层文件,他想确保输入流不是

只是重新给他早先阅读的缓冲区中的内容。



He is not asking the typical "How do I forget whatever is in stdin
question?"

He is asking with an actual file on disk (not a pipe/terminal/etc) that
he performs a read upon, how can he portably and efficiently FORCE an
open read stream to re-read data he wants from disk. The problem being
that a write from another thread/stream/fd/process has changed the
underlying file and he wants to ensure that the input stream doesn''t
just re-give him what is in its buffers from an earlier read.


On Fri,2005年2月11日08:28 :27 -0800,JG写道:
On Fri, 11 Feb 2005 08:28:27 -0800, JG wrote:
有没有人知道标准(或支持Linux,Mac,Win32)的方式来清除读取流缓冲区(标准ANSI C)文件流)?

我甚至会选择平台特定的方式来实现它。

不,我知道我可以使用直接低级I / O或非缓冲做
读取,但对于我的应用程序,我需要缓冲。我可以实现自己,
但这不是最佳的。

例如,我使用fopen()打开一个只读文件。我定期从外部方法知道
底层文件已经更新。
在我下一次读取流之前,我想刷新读取的缓冲区并强制它从中读取磁盘(或内核缓存)。


我能想到的唯一可靠方法是关闭并重新打开流,使用,

,freopen()。

fflush似乎适用于Linux,但不确定它是否标准。


不是。在标准C中,将fflush()应用于输入流调用

未定义的行为。

我没有看到任何函数来执行此操作,但似乎非常需要。


我知道你的意思,虽然令我惊讶的是我不记得需要它了。

我也可以通过fseeking到a触发刷新冲洗文件的不同区域。是否有任何fseek,即使是1指数的offseting
总是强迫缓冲区?
Does anyone know a standard (or supported on Linux, Mac, Win32) way to
clear a read stream buffer (standard ANSI C file stream)?

I would even settle for a platform specific way of doing it.

And no, I know I can use direct low level I/O or non-buffered to do
reads, but for my app, I need the buffering. I can implement myself,
but this is not optimal.

Example, I open a read only file using fopen(). I periodically know
from external methods, that the underlying file has been updated.
Before I do my next read on the stream, I want to flush the read
buffers and force it to read from disk (or kernel cache).
The only sure way I can think of is to close and reopen the stream, using,
say, freopen().
fflush seems to work on Linux, but not sure it is standard.
It isn''t. In standard C applying fflush() to an input stream invokes
undefined behaviour.
I don''t see any function to do this, but it seems very needed.
I know what you mean, although surprisingly I don''t remember needing it.
I can also trigger a flush of the buffers by fseeking to a much
different area of the file. Does any fseek, even offseting by 1 index
always force a flusshing of the buffers?




我希望不会,如果数据在已经在缓冲区中的新职位是

。这种方法似乎是你最好的选择,但是没有关闭并重新打开文件。也许是双重寻求,一个到一些

远离位置然后到你想要的位置。即使这不是100%

保证。


劳伦斯



I hope not, that would be inefficient if the data at the new position was
already in the buffer. This approach seems to be your best bet however
short of closing and reopening the file. Maybe a double seek, one to some
far off position and then to the position you want. Even that isn''t 100%
guaranteed though.

Lawrence


这篇关于清除读取流缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆