尝试从 flatbuffer 的二进制文件访问 "LengthofTable" 时出现 SystemAccessOutOfbound 异常

SystemAccessOutOfbound Exception while trying to access "LengthofTable" from flatbuffer's binary file

我正在根据以下架构存储我的数据。

namespace iCalendarParser;

table EventParsed {

UID:string;
Organizer:string;
Summary:string;
Description:string;
Attendee:[string];
Categories:string;
DtEnd:string;
DtStart:string;
DtStamp:string;
AttendeeCounter:short;

}

table TableofEvents{

  events:[EventParsed];

}

root_type TableofEvents;

我在我的 C++ 应用程序中存储这样的数据。

   std::vector<flatbuffers::Offset<flatbuffers::String>> ListOfAttendee;
   std::vector<flatbuffers::Offset<iCalendarParser::EventParsed>> ListofEvents;



    for (auto EventList : ListofAllEvents)
    {

auto Organizer = builder.CreateString(EventList->Organizer);
auto UID = builder.CreateString(EventList->UID);
auto DtStart = builder.CreateString(EventList->DtStart);
auto DtEnd = builder.CreateString(EventList->DtEnd);
auto DtStamp = builder.CreateString(EventList->DtStamp);
auto Summary = builder.CreateString(EventList->Summary);
auto Description = builder.CreateString(EventList->Description);
auto Categories = builder.CreateString(EventList->Categories);

for (int i = 0; i < EventList->AttendeeCounter; i++)
{
    ListOfAttendee.push_back(builder.CreateString(EventList->Attendee[i]));

}


auto VectorListofAttendee = builder.CreateVector(ListOfAttendee);

auto CreatEventInFlatBuffer = CreateEventParsed(builder, UID, Organizer, Summary, Description, VectorListofAttendee, Categories, DtEnd, DtStart, DtStamp, EventList->AttendeeCounter);

ListofEvents.push_back(CreatEventInFlatBuffer);

 }

 auto CreatVectorOfEventInFlatbuffer = builder.CreateVector(ListofEvents);
 auto InsertEventInFlatBufferList = CreateTableofEvents(builder, 
 CreatVectorOfEventInFlatbuffer);
 builder.Finish(InsertEventInFlatBufferList);

 uint8_t *buf = builder.GetBufferPointer();
 int size = builder.GetSize();

 std::ofstream ofile("icalBin.bin", std::ios::binary);
 ofile.write((char *)&buf, size);
 ofile.close();

我正在尝试从我的 C# 应用程序访问反序列化数据。

byte[] ReadbytesfromFile = File.ReadAllBytes("icalBin.bin");
var buffer = new ByteBuffer(ReadbytesfromFile);
var tableofEvents = TableofEvents.GetRootAsTableofEvents(buffer); 
int len = tableofEvents.EventsLength;

我不知道哪里出了问题。谁能告诉我如何根据模式访问嵌套表并将其反序列化为 C#。

总体思路:

1.Get 事件列表。

2.Access 单个事件结构。 (我想要得到的结果)

我发现你的代码有 2 个问题:

  • 它缺少一个 ListOfAttendee.clear(),您的嵌套循环导致此列表在每次迭代中变大。
  • 您正在将指针数据写入文件,而不是指针指向的内容(删除 &)。