简单的问题:我想将固定长度的字符串写入二进制文件(即二进制文件),如下面的代码片段所示。
写作“看起来”很好,但从文件中读取不起作用(编译和运行没有崩溃,但没有给出预期的结果)。
#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"
}
}
`
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句