How to efficiently read binary data from files that has complex structure in C++

醉酒当歌 提交于 2019-12-05 22:48:33

Use a 64-bit OS and memory map the file. If you need to support a 32-bit OS as well, use a compatibility layer that maps chunks of the file as needed.

Alternatively, if you always need the objects in file order, just write a sane parser to handle the objects in chunks. Like this:

1) Read in 512KB of file.

2) Extract as many objects as possible from the data we read.

3) Read in as many bytes as needed to fill the buffer back up to 512KB. If we read no bytes at all, stop.

4) Go to step 3.

You could mmap some file segments (or the entire file, at least on 64 bits machine). Perhaps use madvise and (in a separate thread) readahead

I guess you already have enough to start off, memory mapping is certainly a neat idea as long as you have enough RAM. Else read in big chunks.

Once the data is available in memory whole file or a big chunk, the simplest way to read is to:

  • define an appropriate struct
  • create a pointer to appropriate offset in the memory where data is loaded
  • reinterpret_cast the pointer to a pointer of type "appropriate struct" or an array of appropriate struct.

You can use #pragmas to ensure the packing size/order etc if needed. But again this would be OS/Compiler dependent.

Well, OK, the header is of variable length, but you have to start somewhere. If you have to read in the whole file first, it can get a bit messy. The whole file can be represented by a struct containing the header up until some length descriptor and then a byte array - you can start there. Once you have the header length, you can set a pointer/length to an array of header entries and then iterate them and so set a pointer/length for an array of file content structs and so on and so on..

All the various arrays of structs would probably need to be packed?

Nasty. I don't really like my own design:(

Anyone got a better idea, other than rewriting the 'separate program' to use a database or XML or something?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!