How does fstream work? Memory or file?

I do not find a clear answer to one aspect of the fstream object needed to determine whether to use it. Does fstream its contents in memory or is it more like a pointer to a location in a file? I originally used CFile and read the text in a CString , but I would prefer not to have the whole file in memory if I can avoid it.

+7
source share
1 answer

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 ).

+5
source

All Articles