C++ C++;从文件的一部分加载数据

C++ C++;从文件的一部分加载数据,c++,serialization,file-io,binaryfiles,boost-serialization,C++,Serialization,File Io,Binaryfiles,Boost Serialization,我想在一个文件中保存一组简单的结构(目前每个结构只有3个整数),并且能够在任何给定的时间读回其中一个结构 作为第一步,我尝试将它们输出到一个文件中,然后使用boost::serialization将它们读回。目前我正在做这件事,它崩溃了: std::array<Patch, 3> outPatches; outPatches[0].ZOrigin = 0; outPatches[0].XOrigin = 0; outPatches[0].Resolution = 64; outP

我想在一个文件中保存一组简单的结构(目前每个结构只有3个整数),并且能够在任何给定的时间读回其中一个结构

作为第一步,我尝试将它们输出到一个文件中,然后使用boost::serialization将它们读回。目前我正在做这件事,它崩溃了:

std::array<Patch, 3> outPatches;

outPatches[0].ZOrigin = 0;
outPatches[0].XOrigin = 0;
outPatches[0].Resolution = 64;

outPatches[1].ZOrigin = 1;
outPatches[1].XOrigin = 5;
outPatches[1].Resolution = 3;

outPatches[2].ZOrigin = 123;
outPatches[2].XOrigin = 546;
outPatches[2].Resolution = 6;

std::ofstream ofs("testing.sss", std::ios::binary);

for (auto const& patch : outPatches)
{
    std::cout << "start archive: " << ofs.tellp() << std::endl;
    {
    boost::archive::binary_oarchive oa(ofs);
    std::cout << "start patch: " << ofs.tellp() << std::endl;

    oa << patch;
    }
}

ofs.close();


std::array<Patch, 3> inPatches;

std::ifstream ifs("testing.sss", std::ios::binary);

for (auto& patch : inPatches)
{
    std::cout << "start archive: " << ifs.tellg() << std::endl;
    {
    boost::archive::binary_iarchive ia(ifs); // <-- crash here on second patch

    std::cout << "start patch: " << ifs.tellg() << std::endl;

    ia >> patch;
    }
}

ifs.close();

for (int i = 0; i != 3; ++i)
    std::cout << "check: " << (inPatches[i] == outPatches[i]) << std::endl;
std::数组输出;
输出[0]。ZOrigin=0;
输出[0]。XOrigin=0;
输出[0]。分辨率=64;
输出[1]。ZOrigin=1;
输出[1]。XOrigin=5;
输出[1]。分辨率=3;
输出[2]。ZOrigin=123;
输出[2]。XOrigin=546;
输出[2]。分辨率=6;
std::ofs流(“testing.sss”,std::ios::binary);
用于(自动常量和修补程序:输出)
{
std::cout我曾经有过一个类似的案例(使用boost/serialization)将文件映射到一个虚拟地址,编写一个在内存缓冲区而不是文件上运行的拖缆,并为我要读取的每个部分分配适当的拖缆偏移量作为缓冲区开始/长度,并使用拖缆初始化iarchive,以便序列化库将其视为每个对象都在一个单独的文件中

当然,添加到文件中需要重新映射。现在我回顾一下,这似乎有点奇怪,但它很有效,afair。

Boost serialization 在boost序列化存档中似乎不可能跳过。到目前为止,我最好的方法是在一个流中使用多个存档:

static const int numPatches = 5000;

std::vector<int> indices(numPatches, 0);
std::iota(indices.begin(), indices.end(), 0);

std::vector<Patch> outPatches(numPatches, Patch());

std::for_each(outPatches.begin(), outPatches.end(), 
    [] (Patch& p)
    {
        p.ZOrigin = rand();
        p.XOrigin = rand();
        p.Resolution = rand();
    });


std::vector<int64_t> offsets(numPatches, 0);

std::ofstream ofs("testing.sss", std::ios::binary);

for (auto i : indices)
{
    offsets[i] = ofs.tellp();

    boost::archive::binary_oarchive oa(ofs, 
        boost::archive::no_header | boost::archive::no_tracking);
    oa << outPatches[i];
}

ofs.close();


std::random_shuffle(indices.begin(), indices.end());


std::vector<Patch> inPatches(numPatches, Patch());

std::ifstream ifs("testing.sss", std::ios::binary);

for (auto i : indices)
{
    ifs.seekg(offsets[i]);

    boost::archive::binary_iarchive ia(ifs,
        boost::archive::no_header | boost::archive::no_tracking);
    ia >> inPatches[i];

    ifs.clear();
}

std::cout << std::all_of(indices.begin(), indices.end(), 
    [&] (int i) { return inPatches[i] == outPatches[i]; }) << std::endl;
static const int numPatches=5000;
标准:向量指数(numPatches,0);
std::iota(index.begin()、index.end()、0);
向量输出(numPatches,Patch());
std::for_each(outPatches.begin()、outPatches.end(),
[](修补程序和程序)
{
p、 佐里金=兰德();
p、 XOrigin=rand();
p、 分辨率=兰德();
});
std::矢量偏移(numPatches,0);
std::ofs流(“testing.sss”,std::ios::binary);
用于(自动i:索引)
{
偏移量[i]=ofs.tellp();
boost::archive::二进制文件,
boost::archive::no_头| boost::archive::no_跟踪);
oa>输入[i];
ifs.clear();
}

这听起来很有趣。据我所知,大部分输入时间都是由iarchive构造函数占用的。(至少,将iarchive移出循环会快10倍,但不会产生正确的结果-所以我想我只是猜测它很慢。)
static const int numPatches = 500;

std::vector<int> indices(numPatches, 0);
std::iota(indices.begin(), indices.end(), 0);

std::vector<Patch> outPatches(numPatches, Patch());

std::for_each(outPatches.begin(), outPatches.end(), 
    [] (Patch& p)
    {
        p.ZOrigin = rand();
        p.XOrigin = rand();
        p.Resolution = 64;
    });


std::vector<int64_t> streamOffset(numPatches, 0);
std::vector<int64_t> streamSize(numPatches, 0);

std::ofstream ofs("testing.sss", std::ios::binary);

PatchBuffer buffer;

for (auto i : indices)
{
    buffer.Clear();

    WriteToPatchBuffer(buffer, outPatches[i]);

    streamOffset[i] = ofs.tellp();
    streamSize[i] = buffer.ByteSize();

    buffer.SerializeToOstream(&ofs);
}

ofs.close();

std::random_shuffle(indices.begin(), indices.end());

std::vector<Patch> inPatches(numPatches, Patch());

std::ifstream ifs("testing.sss", std::ios::binary);

for (auto i : indices)
{
    ifs.seekg(streamOffset[i]);

    buffer.Clear();

    google::protobuf::io::IstreamInputStream iis(&ifs);
    google::protobuf::io::LimitingInputStream lis(&iis, streamSize[i]);
    buffer.ParseFromZeroCopyStream(&lis);

    ReadFromPatchBuffer(inPatches[i], buffer);

    ifs.clear();
}

std::cout << std::all_of(indices.begin(), indices.end(), 
    [&] (int i) { return inPatches[i] == outPatches[i]; }) << std::endl;