简体   繁体   中英

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

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?

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.

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 eg buffer size. In which case, I would expect using setvbuf() to be a pessimisation in all but a few very special cases.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM