So a FILE stream can have both input and output buffers. You can adjust the output stream using setvbuf
(I am unaware of any method to play with the input buffer size and behavior).
Also, by default the buffer is BUFSIZ
(not sure if this is a POSIX or C thing). It is very clear what this means for stdin
/stdout
/stderr
, but what are the defaults for newly opened files? Are they buffered for both input and output? Or perhaps just one?
If it is buffered, does output default to block or line mode?
EDIT: I've done some tests to see how Jonathan Leffler's answer effected real worls programs. It seems that if you do a read then a write. The write will cause the unused portion of the input buffer to dropped entirely. In fact, the there will be some seeks that are done to keep things at the right file offsets. I used this simple test program:
/* input file contains "ABCDEFGHIJKLMNOPQRSTUVWXYZ" */
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *f = fopen("test.txt", "r+b");
char ch;
fread(&ch, 1, 1, f);
fwrite("test", 4, 1, f);
fclose(f);
return 0;
}
resulted in the following system calls:
read(3, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n", 4096) = 27 // attempt to read 4096 chars, got 27
lseek(3, -26, SEEK_CUR) = 1 // at this point, i've done my write already, so forget the 26 chars I never asked for and seek to where I should be if we really just read one character...
write(3, "test", 4) = 4 // and write my test
close(3) = 0
While these are clearly implementation details I found them to be very interesting as far as how the standard library could be implemented. Thanks Jonathan for your insightful answer.