7

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.

2
  • 2
    The macro BUFSIZ defined in <stdio.h> is the default buffer size. There's no standard way to find the buffer size set by setbuf() or setvbuf(). 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(). Commented Feb 4, 2015 at 0:12
  • 1
    There's also a decent chance that until you use the file stream, the buffer size is not set. Until then, you could change the size with setvbuf(), so the library probably doesn't set the buffer size until you try to use it. Add fputc('a', f); after the printf(), and then test the size again; it probably isn't zero any more. Commented Feb 4, 2015 at 0:14

2 Answers 2

12

Note that the correct return type for main() is int, not void.

This code compiles on Linux (Ubuntu 14.04 derivative tested):

#include <stdio.h> #include <stdio_ext.h> int main(void) { FILE *f; size_t bufsize; f = fopen("test.txt", "wb"); if (f == NULL) { perror("fopen failed\n"); return -1; } bufsize = __fbufsize(f); printf("The buffer size is %zd\n", bufsize); putc('\n', f); bufsize = __fbufsize(f); printf("The buffer size is %zd\n", bufsize); fclose(f); return 0; } 

When run, it produces:

The buffer size is 0 The buffer size is 4096 

As suggested in the comments, until you use the file stream, the buffer size is not set. Until then, you could change the size with setvbuf(), so the library doesn't set the buffer size until you try to use it.

The macro BUFSIZ defined in <stdio.h> is the default buffer size. There's no standard way to find the buffer size set by setvbuf(). 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()).

There are numerous small improvements that should be made in the program, but they're not immediately germane.

Sign up to request clarification or add additional context in comments.

3 Comments

Interesting. I'm using a Linux machine as well(OpenSUSE). As a side question, is the value of BUFSIZ architecture specific? I get the value as 32768.
@npn: It varies by O/S for sure. On Mac OS X (10.9.5 Mavericks), I get 1024 for BUFSIZE (a 64-bit compilation). Classically, it was 512; these days, 1024 or 4096 are normal; 32768 is bigger than I've seen before, but not outrageously so.
On my machine (Linux), BUFSIZ doesn't seem to be the default buffer for fread and co. Explicitly setting the buffer size to BUFSIZ yields performance improvements. Strange.
1

__fbufsize man page says:

The __fbufsize() function returns the size of the buffer currently used by the given stream.

so I think this is buffer size used by the stream.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.