7

fstream对于确定它是否值得使用所必需的对象的一个​​方面,我没有找到明确的答案。是否fstream将其内容存储在内存中,还是更像是指向文件中某个位置的指针?我最初是在使用CFile文本并将其读入 aCString中,但如果可以避免的话,我宁愿不要将整个文件放在内存中。

4

1 回答 1

5

fstream is short for file stream -- it's normally a connection to a file in the host OS's file system. (§27.9.1.1/1: "The class basic_filebuf<charT,traits> associates both the input sequence and the output sequence with a file.")

It does (normally) buffer some information from that file, and if you happen to be working with a tiny file, it might all happen to fit in the buffer. In a typical case, however, most of the data will be in a file on disk (or at least in the OS's file cache) with some relatively small portion of it (typically a few kilobytes) in the fstream's buffer.

If you did want to use a buffer in memory and have it act like a file, you'd normally use a std::stringstream (or a variant like std::istringstream or std::ostringstream).

于 2013-06-10T17:11:17.637 回答