Search code examples
cflushfile-descriptor

Empty or "flush" a file descriptor without read()?


(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.


Solution

  • 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