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;
}
I am trying to write a std::string in a file and then reading it back.
Why do i need to resize the string while reading back the text (see the commented line below while reading back the string)? Doesn't the string handles its size automatically?
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
istream::read
的参数指定一个缓冲区,而不是字符串。因此,该函数无法知道存在一个理论上可以指示调整存储大小的对象。因此,调用者必须调整大小。The parameters of
istream::read
specify a buffer, not a string. Therefore, the function cannot know that there is an object that could theoretically be instructed to resize storage. For this reason, the caller has to do the resizing.在C ++ 11之后,
std :: String
保证具有连续的内存缓冲区。引用 cppreference
基本_string的 您需要的尺寸。请注意,
std :: String
在使用其基础缓冲区时不会为您分配缓冲区。但是,我建议使用
std :: String
,而不是将其用作原始缓冲区。因此,您不必手动调整它。例如:使用std :: stringstream
从std :: ifstream
中 拿起缓冲区,您需要事先保留大小。
After C++11,
std::string
is guaranteed to have contiguous memory buffer.Quoted from cppreference
So you may use this characteristic of
std::string
to use it as a buffer as long as you havestd::string
reserve the size you need. Note thatstd::string
won't allocate the buffer for you while using its underlying buffer.However I'd suggest to use
std::string
as is instead of using it as a raw buffer. So you don't have to resize it manually. Eg: usingstd::stringstream
to take the buffer fromstd::ifstream
If you really wants to use
std::string
as raw buffer, you need to reserve the size beforehand.