From: | Jonathan Adamczewski |
Date: | 23 Aug 2001 at 17:01:36 |
Subject: | Re: [amiga-c] Saving and reading structs |
> Hello,
>
> I have a declaration of something like
>struct mystruct
> {
> struct Node nn_Node;
> BOOL a;
> LONG b;
> STRPTR c;
> struct List *d;
> }mystruct;
>
> I allocate STRPTR c as needed so its strlen will vary with each instance of mystruct. I
> wanted to be able to save the struct to disk with fwite(&mystruct, sizeof(struct mystruct)...
> but that won't work because of the varying length. Any suggestions on what's the best way to
> save this?
>
The length of the struct itself doesn't vary - a struct Node, a BOOL, a LONG, a STRPTR and a struct
List* all have a constant length.
The array of chars pointed at by the STRPTR will vary in length.
So, you could just go something like
fwrite( &mystruct, sizeof(struct mystruct)...)
fwrite( mystruct.c, strlen(mystruct.c)+1...) /* which should write a NULL as well */
Alternatively...
The pointers (c and d) won't be very useful (in most cases I can think of) so I would ignore them.
Write the other items to disk, then the NULL terminated string and then the List. Each item (besides
the string) has a fixed size so reading should be easy, while the string is NULL terminated so it should
be easy to determine it's length.
struct Node
BOOL
LONG
struct List
The string whatever it might be\0
You can't do an straight fwrite() in this case, but it would only need a tiny wrapper
fwrite_mystruct( struct mystruct &ms...)
{
struct s_mystruct{
struct Node nn_Node;
BOOL a;
LONG b;
struct List d;
} s_mystruct;
s_myStruct.nn_Node = ms.nn_Node;
s_mystruct.a = ms.a;
s_mystruct.b = ms.b;
s_mystruct.d = *ms.d;
fwrite(s_mystruct, sizeof(struct s_mystruct)...);
fwrite(ms.c, strlen()+1...);
} /* or any other way you want to do this... */
jonathan.
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Do you need to encrypt all your online transactions? Secure corporate intranets? Authenticate your Web sites? Whatever
security your site needs, you'll find the perfect solution here!
http://us.click.yahoo.com/Bre3tC/Q56CAA/yigFAA/dpFolB/TM
---------------------------------------------------------------------~->
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/