During saving to a file go through trex_arr
. Each pointer denotes the beginning of a separate linked list. For each such list you should assign some ID (any number will do, but the neighboring lists should have different IDs). Before saving each trex
structure, you write the ID to the file.
After each structure you will have filenames list (you don't need any IDs for it since you know the number of elements in the list).
Suppose we have a structure like this:
trex_arr[0]: trex (3 filenames) -> trex (2 filenames) -> NULL
trex_arr[1]: trex (2 filenames) -> NULL
trex_arr[2]: trex (1 filenames) -> NULL
As a result, you will have the following in the output file:
ID0
trex0
filename0
filename1
filename2
ID0
trex1
filename0
ID1
trex2
filename0
filename1
ID2
trex3
filename0
Reading data back is fairly simple. You should keep track of the IDs. Once it changes (e.g. from ID0
to ID1
), you need to advance to the next entry of trex_arr
. Also remember that when you are reading the file, the pointers from the structures read are invalid and you should fix them up properly by hand.
Alternatively, you can traverse each linked list till the end during saving to find out its length and store the number of trex
structures before saving every chain.