I have a requirement of waiting using “read” until a buffer is full on an audio codec device. For make it easier, lets take similar example of:
fd= read(fileno(stdin), &buf, 10);
How can I return from the read when I type 10 characters in stdin? (I hope if this is success, I can wait on codec until specified bytes of data is arrived).
The above example needs an “Enter Key” from console, where as I want “read” to unblock only when desired bytes of data is arrived.
EDIT: Requirement is waiting using a single “read” till specified bytes are arrived.
How about this:
Beware that this will block your program until all data is read. The amount of characters read may be less than everything, because there might be an error or the user pressed
CTRL-D(end of file).Also note that the STDIN_FILE file-descriptor is most likely connected to a tty, which means it might be buffered, so doesn’t return data until newline, and that you might have to make the tty unbuffered.
Edit
To make sure the tty connected to stdin is unbuffered, use the following code:
For more information about the
tcsetattrfunction and theICANONflag, check the manual page fortcsetattr.