Mail Archives: djgpp/1997/08/21/08:41:22
Kertis A. Henderson wrote:
>
> Under djgpp, what is the best way to write a bunch of structs to a
> file? For instance, I'd like to write a linked list to a file node by
> node, then be able to read the nodes back and reconstruct the list. A
> node could be something like the following:
>
> struct node {
> int number;
> unsigned char age;
> char *name;
> node *next;
> }
>
> The ints and chars would be relatively easy, but I don't see how a
> variable length string could be written. Of course, the node pointer
> wouldn't be written.
>
> If anybody could help me out (or possibly point me in the right
> direction), I'd appreciate it. Thanks for any input.
>
> --
>
> Kertis Henderson
> kah190 AT psu DOT edu
If you can use C++, try using the String class. First write the length
to disk...
fwrite( str.length(), INT_LENGTH, 1, SomeFile );
Then the string...
fwrite( (const char*)str, 1, str.length(), SomeFile );
Then when you read it again, read the length, then read that many bytes
into a char array, then assign the char array to the string.
There's probably an easier way, but this is how I do it :)
Hope this helps.
Tom Cook
- Raw text -