Quote:
Originally Posted by
laserlight
No. fread reads from stdin into its own buffer. It is likely implemented using operating system specific API calls (and is meant to handle binary data, not just text), but you can imagine it being implemented with getchar (or rather, fgetc). My analogy of reading 20 chars with getchar is pretty much the rough equivalent of calling fread(some_array, 1, 20, stdin).
So, fread doesn't read from stdin's buffer but rather its own. Does this mean that every character that is entered is passed directly to fread's buffer one at a time?
Quote:
Let's suppose that stdin is unbuffered. When you enter those 3 characters and newline, all 4 of them are immediately read by fread and stored in fread's buffer. Since fread expects 10 characters, fread waits to read more characters from stdin. Hence, the program waits for more input.
Let's suppose that stdin is line buffered. When you enter those 3 characters and newline, because there's a terminating newline, all 4 of them are immediately read by fread and stored in fread's buffer. Since fread expects 10 characters, fread waits to read more characters from stdin. Hence, the program waits for more input.
Let's suppose that stdin is fully buffered, with a buffer size of 256. When you enter those 3 characters and newline, none them are read by fread. This time, it is stdin that's waiting for its own buffer to be filled (with another 252 characters) or otherwise flushed. Hence, the program waits for more input.
I understand every scenario you've listed here but since fread reads directly from stdin into its own buffer, the type of buffering stdin uses will not affect my program, is that correct?