Message-ID: <33FC3689.52D3432D@nexus.edu.au> Date: Thu, 21 Aug 1997 22:07:29 +0930 From: Tom Cook MIME-Version: 1.0 To: djgpp AT delorie DOT com Subject: Re: Writing a struct to disk References: <33F93687 DOT 14CE173 AT psu DOT edu> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Precedence: bulk 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