无论如何我可以将数据从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;`
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何在不使用inbetween缓冲区的情况下将整个文件写入字符串流?
我在这里读了很多关于如何完全清除字符串的问题(即重置容量,释放内存).我的问题恰恰相反; 是否有任何可靠的方法来重置字符串(长度),同时保证其容量保持不变?
示例:在循环中重用临时字符串.
如果我这样做,可能会默认发生这种情况
str.clear()
str.reserve(256)
Run Code Online (Sandbox Code Playgroud)
在每个循环迭代中,至少在根据这篇文章的答案使用Visual Studio时:std :: string在visual studio上的特定行为?
但依赖"可能"似乎有点风险.