c++ - 将字符串作为二进制写入/读取到随机访问的文件中
问题描述
简单的问题:我想将固定长度的字符串写入二进制文件(即作为二进制文件),如下面的片段所示。
写作“看起来”很好,但从文件中读取不起作用(编译和运行没有崩溃,但没有给出预期的结果)。
#include <iostream>
#include <fstream>
#include <string>
int main()
{
{
std::ofstream out {"out.dat", std::ios::binary};
//all of length 5
std::string a[] {{"12345"}, {"6789A"}, {"BCDEF"}};
//write down strings without \0
out.write(a[0].c_str(), a[0].length());
out.write(a[1].c_str(), a[1].length());
out.write(a[2].c_str(), a[2].length());
}
{
std::ifstream in {"out.dat", std::ios::binary};
std::string s2 {5, '\0'}; //I can store at least 5 chars
in.seekg(0 + 2 * 5); //try to read string of index 2
in.read(&s2[0], 5);
std::cout << s2 << std::endl; //prints "BC", not "BCDEF"
}
}
最后一行应该恢复字符串“BCDEF”,但只打印“BC”(g++ 10.2.0)。
由于字符串具有固定长度,并且我将指定数量的字节(字符)写入文件,我应该能够恢复它们seekg
(也就是说,我知道它们从哪里开始,并且我可以设置输入位置指示器正确在那里阅读)。
请注意,我不是这个问题的重复,因为我直接写字符(我不需要reinterpret_cast
它们)。
解决方案
这使用带有std::initializer_list
:
std::string s2 {5, '\0'};
...并创建一个包含 2 个字符的字符串,其值为 5 和 0(或任何\0
值)。
它应该是std::string s2 (5, '\0');
例子:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
{
std::ofstream out {"out.dat", std::ios::binary};
//all of length 5
std::vector<std::string> a{{"12345"}, {"6789A"}, {"BCDEF"}};
//write down strings without \0
for(auto& s : a)
out.write(s.c_str(), s.size());
}
{
std::ifstream in {"out.dat", std::ios::binary};
std::string s2(5,'\0'); //I can store at least 5 chars
in.seekg(0 + 2 * 5); //try to read string of index 2
in.read(s2.data(), 5);
std::cout << s2 << std::endl; //prints "BC", not "BCDEF"
}
}
`
推荐阅读
- c# - 如何裁剪具有平滑边框的图像的椭圆区域
- c++ - C ++,使用模板调用成员函数上的线程
- reactjs - “从 2D 数据进行预测”代码实验室在每次刷新时给出随机结果
- c# - 如何在 ac# 类中添加 css bootstrap?
- git - GitHub 找到在 git cli 上找不到的提交
- ios - 删除特定约束后,此错误是什么意思?
- android - 错误 MSB4094:如何归档我的项目?
- python - ImportError:没有名为 geoip2.database 的模块
- express - nodemailer 总是发送相同的响应, res.send() 不起作用
- html - 如何使 Bootstrap Columns 具有相同的高度?