如何将文件内容读入istringstream?
为了提高从文件读取的性能,我试图将一个大(几个MB)文件的全部内容读取到内存中,然后使用istringstream来访问信息。
我的问题是,读取此信息并将其"导入"到字符串流中的最佳方法是什么?这种方法的一个问题(请参见下面的问题)是,在创建字符串流时,缓冲区被复制,并且内存使用量增加了一倍。
#include <fstream>
#include <sstream>
using namespace std;
int main() {
ifstream is;
is.open (sFilename.c_str(), ios::binary );
// get length of file:
is.seekg (0, std::ios::end);
long length = is.tellg();
is.seekg (0, std::ios::beg);
// allocate memory:
char *buffer = new char [length];
// read data as a block:
is.read (buffer,length);
// create string stream of memory contents
// NOTE: this ends up copying the buffer!!!
istringstream iss( string( buffer ) );
// delete temporary buffer
delete [] buffer;
// close filestream
is.close();
/* ==================================
* Use iss to access data
*/
}
解决方案
std :: ifstream具有方法rdbuf(),该方法返回指向filebuf的指针。然后,我们可以将此" filebuf""推送"到" stringstream"中:
int main()
{
std::ifstream file( "myFile" );
if ( file )
{
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
// operations on the buffer...
}
}
编辑:正如马丁·约克在评论中所说,这可能不是最快的解决方案,因为stringstream的operator <<将逐字符读取filebuf。我们可能想检查他的答案,他像以前一样使用ifstream的read方法,然后将stringstream缓冲区设置为指向先前分配的内存。
在我看来,这似乎是过早的优化。处理中正在完成多少工作。假设是现代化的台式机/服务器,而不是嵌入式系统,则在初始化期间复制几MB数据相当便宜,特别是与首先从磁盘读取文件相比。我会坚持使用我们拥有的东西,在系统完成后对其进行测量,然后确定是否值得提高潜在的性能。当然,如果内存紧张,则处于内部循环中,或者经常被调用的程序(例如每秒一次)会改变平衡。
要记住的另一件事是,文件I / O总是最慢的操作。 Luc Touraille的解决方案是正确的,但还有其他选择。一次将整个文件读取到内存中比单独读取要快得多。
好的。我并不是说这比从文件中读取更快
但这是一种创建缓冲区的方法,在将数据读入缓冲区后,直接将其用作字符串流的源。
N.B.值得一提的是std :: ifstream被缓冲了。它从(相对较大)的块中读取文件中的数据。针对缓冲区执行流操作,仅在需要更多数据时才返回文件进行另一次读取。因此,在将所有数据吸入内存之前,请确认这是瓶颈。
#include <fstream>
#include <sstream>
#include <vector>
int main()
{
std::ifstream file("Plop");
if (file)
{
/*
* Get the size of the file
*/
file.seekg(0,std::ios::end);
std::streampos length = file.tellg();
file.seekg(0,std::ios::beg);
/*
* Use a vector as the buffer.
* It is exception safe and will be tidied up correctly.
* This constructor creates a buffer of the correct length.
*
* Then read the whole file into the buffer.
*/
std::vector<char> buffer(length);
file.read(&buffer[0],length);
/*
* Create your string stream.
* Get the stringbuffer from the stream and set the vector as it source.
*/
std::stringstream localStream;
localStream.rdbuf()->pubsetbuf(&buffer[0],length);
/*
* Note the buffer is NOT copied, if it goes out of scope
* the stream will be reading from released memory.
*/
}
}

