空的或“齐平”的。没有 read() 的文件描述符?
(注意:这不是如何刷新write()
的问题。可以这么说,这是它的另一端。 )
是否可以清空包含要读取的数据的文件描述符,而无需 read()
来读取它?您可能对数据不感兴趣,因此阅读全部数据会浪费您可能有更好用途的空间和周期。
如果在 POSIX 中不可能,是否有任何操作系统有任何不可移植的方法来做到这一点?
更新:请注意,我说的是文件描述符,不是流。
(Note: This is not a question of how to flush a write()
. This is the other end of it, so to speak.)
Is it possible to empty a file descriptor that has data to be read in it without having to read()
it? You might not be interested in the data, and reading it all would therefore waste space and cycles you might have better uses for.
If it is not possible in POSIX, do any operating systems have any non-portable ways to do this?
UPDATE: Please note that I'm talking about file descriptors, not streams.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您正在处理 tty,请查看
tcflush()
:http://opengroup.org/onlinepubs/007908775/xsh/ tcflush.html
If you're dealing with a tty, have a look at
tcflush()
:http://opengroup.org/onlinepubs/007908775/xsh/tcflush.html
对于 POSIX,请使用
lseek(2)
或lseek64(3)
向前查找。对于 Windows,请使用SetFilePointer()
或
SetFilePointerEx( )
。For POSIX, use
lseek(2)
orlseek64(3)
to seek ahead. For Windows, useSetFilePointer()
orSetFilePointerEx()
.如果您知道要跳过的字节数,则可以对 POSIX 系统执行 lseek(fd, n, SEEK_CUR); 。还有用于
FILE *
对象的fseek()
。在 POSIX 中,我认为您可以安全地查找文件末尾,其想法是,如果稍后写入更多数据,以便使数据超出使用 lseek() 设置的位置,您将现在能够读取更多数据。If you know the number of bytes to skip, you can do
lseek(fd, n, SEEK_CUR);
for POSIX systems. There isfseek()
as well, forFILE *
objects. In POSIX, I think you can safely seek past the end of file, the idea is that if more data is written later, so as to make data go past the position set withlseek()
, you will be able to read more data now.Linux 2.6.17 或更高版本以及 GNU C 库版本 2.5 或更高版本包含 splice() 系统调用,可用于将数据从一个文件描述符发送到另一个文件描述符,而无需将其复制到用户空间。只需打开
/dev/null
并将源文件描述符中的数据拼接
到/dev/null
中,即可用于丢弃数据文件描述符。Linux 2.6.17 or later with the GNU C library version 2.5 or later contain the
splice()
system call, which can be used to send data from one file descriptor to another without copying it to user space. This can be used to discard data by simply opening/dev/null
andsplice
ing data from the source file descriptor into the/dev/null
file descriptor.流有 fclean 可用,它刷新写缓冲区,并将读缓冲区返回给 IO 系统。
http://www.gnu.org/software/hello/ Manual/libc/Cleaning-Streams.html
如果您真正想要做的是跳过字节,则重新定位文件指针是正确的操作。只需向前跳过您不想阅读的字节即可。
http://www.gnu.org/software/hello/manual/libc/File-Position-Primitive.html#File-Position-Primitive" gnu.org/software/hello/manual/libc/File-Position-Primitive.html#File-Position-Primitive
Streams have fclean available, which flushes the write buffer, and returns the read buffer back to the IO system.
http://www.gnu.org/software/hello/manual/libc/Cleaning-Streams.html
If what you really want to do is skip bytes, repositioning the file pointer is the correct action. Just skip ahead as many bytes as you don't want to read.
http://www.gnu.org/software/hello/manual/libc/File-Position-Primitive.html#File-Position-Primitive
read() 和flush() 都不是标准C 或C++ 的一部分,但当然没有一个标准函数支持输入流的刷新。我猜这反映了底层操作系统中不可用的东西。避免完全阅读某些内容的正常方法是使用某种 search() 函数跳过它。
Neither read() nor flush() are part of Standard C or C++, but certainly none of the standard functions support flushing of input streams. I would guess this reflects something not available in the underlying operating systems. The normal way to avoid reading something altogether is to skip over it with a seek() function of some sort.
根据 this,POSIX 系统将在
上执行此操作fflush(流);
.According to this, a POSIX system will do this on
fflush(stream);
.BSD 引入了
fpurge()
,Solaris 和 glibc 引入了__fpurge()
。从 手册页:
但请注意:
如果您正在使用文件描述符,则可以从描述符获取
FILE*
< /a> 首先。BSD introduced
fpurge()
, Solaris and glibc introduced__fpurge()
.From the man page:
Note, however:
If you're working with a file descriptor, you might be able to get a
FILE*
from the descriptor first.