1

I have a data structure in c that is an array of struct pointers:

struct trex *trex_arr[128];

struct trex{
    struct trex *next;
    char name[LEN];
    unsigned int id; 
    int groups[LEN];
    struct list *filenames; //linked list
    unsigned int fn_len;
};

I want to write the array to file. I understand that I need to write the object in the "next" pointer and each element in the linked list as well, but how do I do it so that I can read it back successfully into my original array (there is chaining in the array as well)?

user1190650
  • 3,207
  • 6
  • 27
  • 34
  • Easiest way would be serializing and deserializing it to XML but for C case you need a library support for it. How about using headers? Then you know you encounter with the linked list of the object, and create your linked list accordingly. Is this your question or I misunderstood it? – Halil Sep 28 '12 at 14:06
  • What exactly do you mean by using headers? – user1190650 Sep 28 '12 at 14:09
  • On your output text file, put some identifiers that indicates a beginning of a linked list. It should be unique from the data that is hold so that whenever you encounter such data, your program will know linked list is starting. – Halil Sep 28 '12 at 14:11
  • Does `trex_arr` keep pointers each of which is the beginning of a new list? Can there be a case when `trex_arr[3] == trex_arr[50]->next`? – Maksim Skurydzin Sep 28 '12 at 14:14
  • @MaximSkurydin No, there are no loops in the linked list or chain – user1190650 Sep 28 '12 at 14:17

3 Answers3

1

Offhand, the simplest way to do it is to write the structure out sequentially so that:

[trex item #1]
[list item #1]
[list item #2]
[list item #3]
[trex item #2]
[list item #1]
[list item #2]
[list item #3]
[list item #4]
etc...

(Of course, this is just sort of the jist of it. These are binary writes and this is a visual only.)

I'm hoping that fn_len describes the number of items in the linked list struct list *filenames. If so, this is a snap.

On writing:

  1. Write out the first structure in the trex linked list
  2. Loop and write out the struct list *filenames from that first trex structure one at a time
  3. Repeat #1 with the next item in the trex linked list until the list is exhausted.

On reading, remember: all of your pointers are going to be worthless initially. You have to stitch them together yourself. But the structures are arranged in the correct order in the file.

  1. Read the first trex structure. You know its size, reading it isn't a problem. Fix the next pointer to nothing.
  2. Loop and read each of the struct list *filenames one at a time. You know how many they are because of fn_len in the structure you just read. Stitch together that linked list and attach it to the trex structure.
  3. Repeat #1 with the next trex structure until EOF, join the structure onto the end of the linked list as appropriate.

And that should take care of it.

Clinton Pierce
  • 12,859
  • 15
  • 62
  • 90
  • Thanks so much for the post- that's really helpful. Just to clarify, since each trex struct has a pointer to next trex struct (so we have a 2D array-like structure), I don't know how long each chain of trex structs is starting at each array index. Do you think it would be easiest to keep a list of how long each chain is? – user1190650 Sep 28 '12 at 14:25
  • You shouldn't have to know how long the list of `trex` is. The only reason it's handy to know how long each chain of `struct list` is so that when reading back, you don't have to decide if the next read is the next 'trex' or the next 'struct list'. You read each `struct list` until you've reached `fn_len` from the previous `trex`, and you read `trex` until EOF. – Clinton Pierce Sep 28 '12 at 16:56
0

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.

Maksim Skurydzin
  • 10,301
  • 8
  • 40
  • 53
-1

I am not quite sure what your asking but if I understood right you just want to be able to go back to the beginning of the list when you reach the end after writing in a file all that you find?

What I would do is stock the address of the first structure of the list so that you can go back to the beginning any time you want. The other solution would be to make a prev/next doubly chained list so that you can go back and forth without any problems. Set your like in prev of your 1st link to 0 so that you can know it is the beginning.

If you could give more details I could be more helpful but not quite sure what your asking.

Jp1987
  • 45
  • 1
  • 8
  • The problem is about saving the entire structure to the file and reading it back. – Maksim Skurydzin Sep 28 '12 at 14:15
  • Ah ok I see. Well the best solution would either to save it to an xml or xml like file. Either download a lib or make your own. – Jp1987 Sep 28 '12 at 14:45
  • What libraries work well for this type of XML output? I think writing out each struct and linked list as described above will work well, but I wonder if writing to/from XML will be a simple solution. – user1190650 Sep 28 '12 at 14:48
  • http://stackoverflow.com/questions/399704/xml-parser-for-c Lib's do all the work. You give it values and it generates the xml. And parsers are inculded. – Jp1987 Sep 28 '12 at 14:50