So I do have a binary file, that i want to parse. In this file I have a header and the header defines how the rest of the file is structured.
For example the file would look like this (as a mock up):
Version_Number = 001
File_Type = config
Number_of_struct_A = 4
Number_of_struct_B = 3
struct_A
struct_A
struct_A
struct_A
struct_B
struct_B
struct_B
And so far I am able to read the first structure in my file, as shown in this example: Parsing binary data into ctypes Structure object via readinto()
So for example I will have the following types of structures:
class FileInfo(Structure):
_fields_ = [("Versions_Nr", c_byte),
("File_Typ", c_byte),
("Number_of_struct_A", c_byte),
("Number_of_struct_B", c_byte)]
class Struct_A(Structure):
_fields_ = [("A", c_byte),
("B", c_byte),
]
class Struct_B(Structure):
_fields_ = [("C", c_byte),
("D", c_byte),
]
#read in my file
file = open('test.cfg', 'rb')
header = FileInfo()
file.readinto(header)
print(header.Number_of_struct_A)
So this will give me the header, but i have no idea how I should save the data from my file, so that i can use the information about the number of structures, that i just gained. Or for that matter, how to store the data at all, as reading the file over and over again doesn't seem to be a good option.
I hope my question is understandable.
fileagain does exactly what you want—it reads the next few bytes rather than reopening the file. (That’s whyopenis its own function!)