views:

52

answers:

4

Hi, I have a complex structure in a C program which has many members that are dynamically allocated memory. How do I write this structure to a text / binary file? How will I be able to recreate the entire structure from the data read from the file.

struct parseinfo{
 int  varcount;
 int  termcount;
 char **variables;
 char **terminals;
 char ***actions;
};

The members variables, terminals and actions are all dynamically allocated and I need to write this structure to a file so that I could reconstruct the structure later.

+4  A: 

You must write to this structure Serialize and Deserialize functions, you can't write this structure to file as raw data because you have allocated pointers on a heap and this not make sense to save this values on file.

Svisstack
+1. Some kind of serialization/deserialization logic is required. [Google Protocol Buffers](http://code.google.com/p/protobuf/) is one library for this purpose that works in C++. I'm sure there are many others as well.
Daniel Pryden
I understand that there is a need to serialize my structure. For now I have settled for a simple format in which I store 'varcount' then every row of 'variables' is stored on a new line. Similarly for other fields. I googled a lot a figured out that XML and JSON are what I was looking for. I will modify my source to use one of these in the future. Thank you for the reply.
A: 

The way I would suggest to do it is to think about what does it take to create the items in the first place?

When it comes to the char **variables; char **terminals; char **actions you're obviously going to have to figure out how to declare those and read them in but I don't think you can inject a /0 into a file (EOF character??)

How would you like to see it written to the file? Can you provide a sample output how you think it should be stored? Perhaps one item per line in a file? Does it need to be a binary file?

drachenstern
You can in fact write `'\0'` bytes, or any other `char` values you want, into a file. If you do, it may not be useful to try to view the file in a viewer meant for text files. Otherwise, this post asks some good questions to get your thinking started.
aschepler
Hi @aschepler, is that at me or at him? I wasn't sure about that `\0` so thanks for that.
drachenstern
Thank you drachenstern.There are two choices-- Use my own format by storing field widths followed by data OR use a universal format for for arbitrary data stuctures like XML or JSON. Because I do not have much time at hand I am using my own format and looking forward to use XML or JSON in future.
+1  A: 

Short: Not in an automated way.

In essence, it depends highly on the semantics of your structure. If the data fields inside specify the length of certain arraiys inside it, you can reconstruct the struct. But you have to be careful to "beleive" the values. (possible cause of stack overflow (nice word)) if you believe that there are 2^34 entries in an array. But otherwise it is just going tru every member (pain)

You could search a little about ASN.1 and TLV-structs.

RobKop
Avoid ASN.1 if you can! it's not fun to use.
Blank Xavier
Thanks a lot RobKop for now I'm doing what you suggested.
A: 

Here are some suggestions for binary serialization:

You can serialize a string either a la C (write the terminating '\0') or by writing the size (say, an int) followed by the contents.

You can serialize an array of strings by writing the length of the array followed by the strings.

If it's possible that you deserialize the file on a different architecture (different int size, different endianness...), then take care to carefully specify the binary format of the file. In such a case you may want to take a look at the XDR serialiaztion standard.

For ASCII serialization, I like the JSON format.

Edgar Bonet
A big thank you Edgar. I found out that JSON (or XML) suits my needs because I'm storing only ASCII data. Thanks again.