From: | Jonathan Adamczewski |
Date: | 23 Aug 2001 at 18:47:38 |
Subject: | Re: [amiga-c] Re: Saving and reading structs |
>> 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?
Sounds about right...
fgets a line
[0..(sizeof(struct)-1)] will be the struct
[sizeof(struct)..endOfLine-1] will be terminated string.
Read up on fgets to make sure you handle NULLs and newlines correctly...
(nothing tricky - fgets returns a NULL terminated string. If the line ended with a \n rather than a EOF
the \n is included in the string - so long as the line read is shorter than the buffer provided :)
You would need to make one adjustment to the above function though - fgets uses newline to break up
the file, so you would need to write one after the string. (eg you could replace the NULL with a \n one
way or another)
Using streams and strings in c++ is so much nicer :)
jonathan.
------------------------ 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/