空的或“齐平”的。没有 read() 的文件描述符?

发布于 2024-08-15 10:17:44 字数 304 浏览 5 评论 0原文

(注意:这不是如何刷新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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

[浮城] 2024-08-22 10:17:44

如果您正在处理 tty,请查看 tcflush()

#include <termios.h>
int tcflush(int fildes, int queue_selector);

成功完成后,tcflush()
丢弃写入对象的数据
fildes 引用(一个打开的文件
与终端关联的描述符)
但未发送或已收到数据
但不读,取决于值
队列选择器 [...]

http://opengroup.org/onlinepubs/007908775/xsh/ tcflush.html

If you're dealing with a tty, have a look at tcflush():

#include <termios.h>
int tcflush(int fildes, int queue_selector);

Upon successful completion, tcflush()
discards data written to the object
referred to by fildes (an open file
descriptor associated with a terminal)
but not transmitted, or data received
but not read, depending on the value
of queue_selector [...]

http://opengroup.org/onlinepubs/007908775/xsh/tcflush.html

梦途 2024-08-22 10:17:44

对于 POSIX,请使用 lseek(2)lseek64(3) 向前查找。对于 Windows,请使用 SetFilePointer()SetFilePointerEx( )

For POSIX, use lseek(2) or lseek64(3) to seek ahead. For Windows, use SetFilePointer() or SetFilePointerEx().

2024-08-22 10:17:44

如果您知道要跳过的字节数,则可以对 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 is fseek() as well, for FILE * 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 with lseek(), you will be able to read more data now.

咿呀咿呀哟 2024-08-22 10:17:44

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 and spliceing data from the source file descriptor into the /dev/null file descriptor.

杀お生予夺 2024-08-22 10:17:44

流有 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

半﹌身腐败 2024-08-22 10:17:44

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.

公布 2024-08-22 10:17:44

根据 this,POSIX 系统将在 上执行此操作fflush(流); .

对于打开读取的流,如果文件尚未位于 EOF,并且该文件能够查找,则应调整底层打开文件描述的文件偏移量,以便对打开文件描述进行下一个操作处理从正在刷新的流中读取或写入的最后一个字节之后的字节。

According to this, a POSIX system will do this on fflush(stream); .

For a stream open for reading, if the file is not already at EOF, and the file is one capable of seeking, the file offset of the underlying open file description shall be adjusted so that the next operation on the open file description deals with the byte after the last one read from or written to the stream being flushed.

孤寂小茶 2024-08-22 10:17:44

BSD 引入了 fpurge(),Solaris 和 glibc 引入了 __fpurge()
手册页

#include <stdio.h>
#include <stdio_ext.h>
void  __fpurge(FILE *stream);

函数 __fpurge() 清除给定流的缓冲区。对于输出流,这会丢弃任何未写入的输出。对于输入流,这会丢弃从底层对象读取但尚未通过 getc(3) 获得的任何输入;这包括通过 ungetc(3) 推回的任何文本。

函数 __fpurge() 的作用完全相同,但不返回值。

但请注意:

这些函数是非标准且不可移植的。函数 fpurge() 是在 4.4BSD 中引入的,在 Linux 下不可用。函数 __fpurge() 是在 Solaris 中引入的,并且存在于 glibc 2.1.95 及更高版本中。

如果您正在使用文件描述符,则可以从描述符获取FILE*< /a> 首先。

int fd;
/* ... */
FILE* fp = fdopen(fd, "r");
/* ... */
__fpurge(fp);

BSD introduced fpurge(), Solaris and glibc introduced __fpurge().
From the man page:

#include <stdio.h>
#include <stdio_ext.h>
void  __fpurge(FILE *stream);

The function __fpurge() clears the buffers of the given stream. For output streams this discards any unwritten output. For input streams this discards any input read from the underlying object but not yet obtained via getc(3); this includes any text pushed back via ungetc(3).

The function __fpurge() does precisely the same, but without returning a value.

Note, however:

These functions are nonstandard and not portable. The function fpurge() was introduced in 4.4BSD and is not available under Linux. The function __fpurge() was introduced in Solaris, and is present in glibc 2.1.95 and later.

If you're working with a file descriptor, you might be able to get a FILE* from the descriptor first.

int fd;
/* ... */
FILE* fp = fdopen(fd, "r");
/* ... */
__fpurge(fp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文