Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++ ofstream file writing use a buffer?

Tags:

c++

file

file-io

People also ask

What is a file buffer C++?

A buffer is temporary storage of data that is on its way to other media or storage of data that can be modified non-sequentially before it is read sequentially. It attempts to reduce the difference between input speed and output speed.

Does ofstream create a file?

To create a file, use either the ofstream or fstream class, and specify the name of the file. To write to the file, use the insertion operator ( << ).

When using an ofstream what happens when the output file name already exists?

If the file already exists, the old version is deleted. Appending records to an existing file or creating one if it doesn't exist. ofstream ofile("FILENAME", ios::app); Opening two files, one at a time, on the same stream.


Yes, all stream operations are buffered, though by default the standard input, output and error output are not so that interactions with the C IO is less surprising.

As already alluded, there is a base class streambuf that is used behind the scenes. It is provided with its own buffer, which size is an implementation detail.

You can check (experimentally) how much this buffer is by using streambuf::in_avail, assuming that input filestream and output filestream are setup with the same buffer size...

There are two other operations that you can do here that might be of interest:

  • you can change the streambuf object used by a stream, to switch to a custom version
  • you can change the buffer used by the streambuf object

both should be done either right after creating the stream or after a flush, lest some data is lost...

To illustrate the buffer change, check out streambuf::putsetbuf:

#include <fstream>
#include <vector>

int main () {
  std::vector<char> vec(512);

  std::fstream fs;
  fs.rdbuf()->pubsetbuf(&vec.front(), vec.size());

  // operations with file stream here.
  fs << "Hello, World!\n";

  // the stream is automatically closed when the scope ends, so fs.close() is optional
  // the stream is automatically flushed when it is closed, so fs.flush() is optional

  return 0;
}

Now you can repeat the experiments you did in C to find the sweet spot :)


Yes, ostreams use a stream buffer, some subclass of an instantiation of the template basic_streambuf. The interface of basic_streambuf is designed so that an implementation can do buffering if there's an advantage in that.

However this is a quality of implementation issue. Implementations are not required to do this but any competent implementation will.

You can read all about it in chapter 27 of the ISO standard, though maybe a more readable source is The C++ Standard Library: A Tutorial and Reference (google search).


Per this, ofstream has an internal filebuf pointer, can be read through the rdbuf function, which points to a streambuf object, which is this:

streambuf objects are usually associated with one specific character sequence, from which they read and write data through an internal memory buffer. The buffer is an array in memory which is expected to be synchronized when needed with the physical content of the associated character sequence.

I bolded the important bits, it seems that it does make use of a buffer, but I don't know or haven't found out what kind of buffer it is.