I'm trying to write a small program to find the buffer size of an open file stream. After searching around a little bit, I found the __fbufsize() function. This is the code I wrote:
#include <stdio.h> #include <stdio_ext.h> void main() { FILE *f; int bufsize; f = fopen("test.txt","wb"); if (f == NULL) { perror("fopen failed\n"); return; } bufsize = __fbufsize(f); printf("The buffer size is %d\n",bufsize); return; } I get the buffer size as zero. I'm a bit confused as to why this is happening. Shouldn't the stream be buffered by default? I get a non-zero value if I use setvbuf with _IOFBF before calling fbufsize.
BUFSIZdefined in<stdio.h>is the default buffer size. There's no standard way to find the buffer size set bysetbuf()orsetvbuf(). You need to identify the platform you're working on to allow useful commentary on__fbufsize()as a function (though it seems to be a GNU libc extension:__fbufsize().setvbuf(), so the library probably doesn't set the buffer size until you try to use it. Addfputc('a', f);after theprintf(), and then test the size again; it probably isn't zero any more.