将字符串作为二进制写入/读取到随机访问的文件中

简单的问题:我想将固定长度的字符串写入二进制文件(即二进制文件),如下面的代码片段所示。

写作“看起来”很好,但从文件中读取不起作用(编译和运行没有崩溃,但没有给出预期的结果)。

#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 
        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, ''}; //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, ''};

...并创建一个包含 2 个字符的字符串,其值为 5 和 0(或任何值)。

它应该是 std::string s2 (5, '');

例子:

#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 
        for(auto& s : a)
            out.write(s.c_str(), s.size());
    }

    {
        std::ifstream in {"out.dat", std::ios::binary};

        std::string s2(5,''); //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"  
    }
}

`


以上是将字符串作为二进制写入/读取到随机访问的文件中的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>