【发布时间】:2011-05-03 03:37:53
【问题描述】:
我是否可以将数据从fstream(文件)传输到stringstream(内存中的流)?
目前,我使用的是缓冲区,但这需要双倍的内存,因为你需要将数据复制到缓冲区,然后将缓冲区复制到字符串流,直到你删除缓冲区,数据才会复制到记忆。
std::fstream fWrite(fName,std::ios::binary | std::ios::in | std::ios::out);
fWrite.seekg(0,std::ios::end); //Seek to the end
int fLen = fWrite.tellg(); //Get length of file
fWrite.seekg(0,std::ios::beg); //Seek back to beginning
char* fileBuffer = new char[fLen];
fWrite.read(fileBuffer,fLen);
Write(fileBuffer,fLen); //This writes the buffer to the stringstream
delete fileBuffer;`
有谁知道如何在不使用中间缓冲区的情况下将整个文件写入字符串流?
【问题讨论】:
-
有什么意义?您是否正在尝试提高吞吐量?在这种情况下,您可能需要放弃
fstream,iostream 很慢。您是否正在尝试减少内存占用?分块读取文件而不是一次性读取文件可能会有所帮助。
标签: c++ stl buffer fstream stringstream