Amiga-C (174/223)

From:Jack York
Date:23 Aug 2001 at 08:25:31
Subject:[amiga-c] Re: Saving and reading structs

Hello Jonathan

On 23-Aug-01, Jonathan Adamczewski wrote:
>> 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?
>>

> 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... */

Hmm, hadn't thought of this. Would I read in the file using fgets and parse
the line based on the sizes of the struct members?

Jack



E-Mail: jyork@twmi.rr.com
Alt: jyork@voyager,net
Home: http://my.voyager.net/~jyork

------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get VeriSign's FREE GUIDE: "Securing Your Web Site for Business." Learn about using SSL for serious online security. Click Here!
http://us.click.yahoo.com/KYe3qC/I56CAA/yigFAA/dpFolB/TM
---------------------------------------------------------------------~->

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/