Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 642
Teddy Avatar asked Dec 17 '09 19:12

Teddy


2 Answers

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

like image 180
squelart Avatar answered Oct 19 '22 02:10

squelart


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

like image 21
Adam Rosenfield Avatar answered Oct 19 '22 03:10

Adam Rosenfield