std::string需要显式调整大小还是自己处理调整大小



我正试图在一个文件中写入一个std::字符串,然后将其读回。为什么我在读回文本时需要调整字符串的大小(读回字符串时请参阅下面的注释行(?字符串不是自动处理其大小吗?

#include <iostream>
#include <fstream>
int main()
{
{
std::ofstream ofile("c:\out.txt", std::ios_base::binary);
if (!ofile.is_open())
{
std::cout << "Failed to open the file";
return 1;
}
std::string s = "Hello World";
try
{
ofile.write(s.data(), s.size());
if (ofile.fail())
{
std::cout << "Failed to write the file";
return 1;
}
}
catch (std::ios_base::failure& e)
{
std::cout << e.what();
}

ofile.close();
}
{
std::ifstream ifile("c:\out.txt", std::ios_base::binary);
if (!ifile.is_open())
{
std::cout << "Unable to open input file";
return 1;
}
ifile.seekg(0, std::ios::end);
auto length = ifile.tellg();
ifile.seekg(0, std::ios::beg);
std::string outstr;
//outstr.resize(length);
try
{
ifile.read(reinterpret_cast<char*>(&outstr.front()), length);
}
catch (std::ios_base::failure& e)
{
std::cout << e.what();
}
std::cout << outstr;
}
return 0;
}

istream::read的参数指定缓冲区,而不是字符串。因此,函数无法知道是否存在理论上可以指示调整存储大小的对象。由于这个原因,调用者必须调整大小。

在C++11之后,std::string保证具有连续的内存缓冲区。

引用自cppreference

basic_string的元素被连续存储在中

因此,只要std::string保留了所需的大小,就可以使用std::string的此特性将其用作缓冲区。请注意,std::string在使用其底层缓冲区时不会为您分配缓冲区。

然而,我建议按原样使用std::string,而不是将其用作原始缓冲区。所以你不必手动调整它的大小。例如:使用std::stringstreamstd::ifstream获取缓冲区

// ... open ifstream ifile...
std::stringstream ss;
ss << ifile.rdbuf();
// Use ss.str() to get `std::string`

如果您真的想使用std::string作为原始缓冲区,则需要事先预留大小。

// ... 
std::string outstr(length, ' '); // or call `outstr.resize()` after
ifile.read(&outstr[0]), length);

最新更新