Is there any need to create my own buffer for file I/O operations in C/C++?

Go To


Is there any need to use a custom buffer to either read or write files in C or C++ to reduce file I/O?

For example, if you need to read a file entry by entry (one char at a time, or one struct at a time), is it recommended to reduce the number of calls to fread() using a buffer? Does it make any difference in I/O (for read and write)? Does the answer depend on the Operational System or anything else not in the code?

I've learned that this was recommended, but today someone told me about setvbuf() on stdio.h, and it seems that everything is already there and you don't need to add this complexity to your program.

Looking at stackoverflow, I've found an answer with no votes claiming that there is no significant difference between using fgetc/setvbuf() vs. fgets. Is that really true?

2012-04-04 06:46
by dbarbosa
You should probably benchmark this yourself, as it might differ a lot between use-cases - Some programmer dude 2012-04-04 06:59


The functions in <stdio.h> all do their own buffering. There are exceptions, but generally, I would expect them to be optimized for the system they're running on, with regards to e.g. buffer size. In which case, I would expect using setvbuf() to be a pessimisation in all but a few very special cases.

2012-04-04 07:26
by James Kanze


The fread() function already implements buffering to avoid calling the lower-level read() too often. You shouldn't worry about it unless you do some benchmarks and find out that file I/O is taking a lot of time.

2012-04-04 07:08
by David Grayson


The std::istream object requres a std::streambuf object associated with it to actually perform read operations.

The implementation for file of istream (ifstream) internally has a fstreambuf that does just that.

2012-04-04 07:26
by Emilio Garavaglia