fstream not suitable for file stream - this is usually a connection to a file in the host file system. (Β§27.9.1.1 / 1: "The basic_filebuf<charT,traits> class associates both an input sequence and an output sequence with a file.")
It (usually) buffers some information from this file, and if you work with a tiny file, it can happen that it will go into the buffer. However, in a typical case, most of the data will be in a file on disk (or at least in the OS file system cache) with some relatively small part (usually several kilobytes) in the fstream buffer.
If you want to use a buffer in memory and make it act like a file, you usually use std::stringstream (or an option like std::istringstream or std::ostringstream ).
Jerry Coffin
source share