Consider the following line of code:
while((n = read(STDIN_FILENO, buff, BUFSIZ)) > 0)
As per my understanding read/write
functions are a part of non-buffered I/O. So does that mean read()
function will read only one character per call from stdio? Or in other words, the value of n will be
-1 in case of error
n = 0 in case of EOF
1 otherwise
If it is not the case, when would the above read()
function will return and why?
Note: I was also thinking that read()
will wait until it successfully reads BUFSIZ
number of characters from stdin. But what happens in a case number of characters available to read are less than BUFSIZ
? Will read wait forever or until EOF arrives (Ctrl + D
on unix or Ctrl + Z
on windows)?
Also, lets say BUFSIZ = 100
and stdin = ACtrl+D
(i.e EOF immediately following a single character). Now how many times the while loop
will iterate?