Use pointer to write and read structures

I am working on a mailbox project, and I have these two structures:

struct mmbox_mail

struct mmbox_mail {
char *sender, *recipient;
char *obj, *date;
char flags;
size_t size;
};

and

mail_t

typedef struct{
struct mmbox_mail info;
void *body;
void *next;
} mail_t;

I cannot modify the fields of the structure because I need variable data (for this I use char * instead of char []).

Each mail_t structure is a mail. I need to save each user’s mail in a file, which can be a binary file or a text file (but I think it’s better to use a binary file because I have a The void * body of the text saved.

I tried to do this, but it seems it doesn’t work:

while(mailtmp != NULL){
fwrite(mailtmp, sizeof(mail_t), 1, fp);

/* next mail */
mailtmp=mailtmp->next;
}

06003

Can you help me? I tried to search everywhere, but I never found anyone who asked to save two structures, one inside the other.

Of course, this will not copy the size of the pointer like a string (usually 4 characters Section). I see 3 options here:

>Serialized data, binary file (http://en.wikipedia.org/wiki/Serialization).
>Create A format to be in a text file Store data.
>Use markup languages ​​such as XML/JSON

In any case, you need to traverse each field of the structure to write it into the data file. As for reading, in the first two In this case, you must read the data exactly in the order in which the data was written. In the third case, you can read the fields independently in any order.

If you choose the first method, for each string (char *) field, and write zero termination bytes, so that you always know where it ends when you read it.

I am working on a mailbox project, I have these two structures:

struct mmbox_mail

struct mmbox_mail {
char *sender, *recipient;
char *obj, *date;
char flags;
size_t size;
};

and

mail_t

typedef struct{
struct mmbox_mail info;
void *body;
void *next;
} mail_t;

Me I can’t modify the fields of the structure because I need variable data (for this I use char * instead of char []).

Each mail_t structure is an email. I need to add each user’s The mail is saved in a file, which can be a binary file or a text file (but I think it is better to use a binary file because I have a void * body that is difficult to save in plain text.

I tried to do this , But it seems it doesn’t work:

while(mailtmp != NULL){
fwrite(mailtmp, sizeof(mail_t), 1, fp);

/* next mail */
mailtmp=mailtmp->next;
}

06003

Can you help me? I tried to search everywhere, but I never found someone who asked to save two structures, one inside the other.

Of course, this will not look like Copy the size of the pointer like a string (usually 4 bytes). I see 3 options here:

>Serialized data, binary file (http://en.wikipedia .org/wiki/Serialization).
>Create a format to store data in text files.
>Use markup languages ​​such as XML/JSON

In any case, you need Traverse each field of the structure to write it into the data file. As for reading, in the first two cases, you must read the data exactly in the order in which the data was written, and in the third case, you can read the fields independently in any order .

If you choose the first method, for each string (char *) field, also write a zero-terminated byte so that you always know where it ends when you read it. < /p>

Leave a Comment

Your email address will not be published.