Thanks for your reply laserlight!
fread has its own buffer, i.e., the memory of the objects that you are trying to read into. So, fread doesn't have to read everything at once, and so it doesn't have to know beforehand how many characters are waiting to be consumed from the input stream. It just has to read what it can until its buffer is full, or EOF or an error is reached. If the input stream that it is reading from is buffered (whether line buffered or fully buffered), then that means it may have to wait (possibly multiple times) until that input buffer is flushed.
I'm still confused. Does this mean that characters will be passed from stdin's buffer to fread's buffer then fread will read from its own buffer?
As an analogy, try writing a program to read 20 chars from stdin into a string by calling getchar in a loop until either 20 chars have been read or EOF is reached, then null terminate the string and print it. Enter a line of 10 chars. You will find that the string is not yet printed. Does this mean that stdin knows that 20 chars should be entered, hence stdin is fully buffered? No, it is because of your loop condition.
I completely understand. I'll post a small program to help show what's tripping me up.
Code:
#include <stdio.h>
#define SIZE 10
int
main(void)
{
char buff[SIZE];
size_t bytes;
bytes = fread(buff, 1, SIZE, stdin);
fwrite(buff, 1, bytes, stdout);
putchar('\n');
return 0;
}
Control doesn't seem to be passed to fwrite to print the contents of buff until SIZE has been reached including a newline. So, if I type in 3 characters plus <enter> nothing happens. The program is still waiting for more input. Only when I enter 9 or more characters plus <enter> does the program continue on to print the contents of buff. This is the part I don't understand. If the characters I'm typing are first stored into stdin's buffer, how is it it knows to stop input at or after SIZE?