Thus, a FILE stream can have both input and output buffers. You can configure the output stream using setvbuf (I don't know about any way to play with the size and behavior of the input buffer).
Also, the default buffer is BUFSIZ (not sure if it is a POSIX or C object). It is very clear what this means for stdin / stdout / stderr , but what are the default values ββfor newly opened files? Are they buffered for both input and output? Or maybe just one?
If it is buffered, does it default to block or row mode?
EDIT: I did some tests to find out how Jonathan Leffler responds to real world programs. It seems that if you read, then write. Writing will cause the unused portion of the input buffer to drop completely. In fact, there will be some attempts to be made to keep things at the correct file offsets. I used this simple test program:
#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; }
led to the following system calls:
read(3, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n", 4096) = 27
Although these are clearly implementation details, I found them very interesting as far as the standard library could be implemented. Thanks to Jonathan for your insightful answer.
c file stream buffer
Evan teran
source share