std::fstream buffering vs manual buffering (why 10x gain with manual buffering)?

This is basically due to function call overhead and indirection. The ofstream::write() method is inherited from ostream. That function is not inlined in libstdc++, which is the first source of overhead. Then ostream::write() has to call rdbuf()->sputn() to do the actual writing, which is a virtual function call. On top of that, libstdc++ redirects sputn() … Read more

Why is failbit set when eof is found on read?

The failbit is designed to allow the stream to report that some operation failed to complete successfully. This includes errors such as failing to open the file, trying to read data that doesn’t exist, and trying to read data of the wrong type. The particular case you’re asking about is reprinted here: char buffer[10]; stream.read(buffer, … Read more

Retrieving file descriptor from a std::fstream [duplicate]

You can go the other way: implement your own stream buffer that wraps a file descriptor and then use it with iostream instead of fstream. Using Boost.Iostreams can make the task easier. Non-portable gcc solution is: #include <ext/stdio_filebuf.h> { int fd = …; __gnu_cxx::stdio_filebuf<char> fd_file_buf{fd, std::ios_base::out | std::ios_base::binary}; std::ostream fd_stream{&fd_file_buf}; // Write into fd_stream. // … Read more

std::fstream doesn’t create file

You’re specifying std::fstream::in in your call to fstream::open(). This is known to force it to require an existing file. Either remove std::fstream::in from your mode argument, or specify std::fstream::trunc in addition to the other flags.