Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer size in file I/O

Tags:

c

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.

like image 234
npn Avatar asked Feb 04 '15 00:02

npn


People also ask

What is file buffer?

A file buffer is the temporary image of the file that you can edit. You can edit the file buffer without affecting the original file, until you save it using the Save command. The File > Save command writes the file buffer contents back over the original file. This is the only time the original file is changed.

Is bigger buffer better?

A larger buffer size reduces the potential for flow control to occur, and results in improved CPU utilization. However, a large buffer size can have a negative effect on performance in some cases. If the TCP/IP buffers are too large and applications are not processing data fast enough, paging can increase.

How big should buffer be?

A good buffer size for recording is 128 samples, but you can also get away with raising the buffer size up to 256 samples without being able to detect much latency in the signal. You can also decrease the buffer size below 128, but then some plugins and effects may not run in real time.


1 Answers

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.

like image 76
Jonathan Leffler Avatar answered Oct 15 '22 14:10

Jonathan Leffler