How Do I fwrite() and fread() A Structure that contains pointers to dynamic data 
Author Message
 How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

I have a question, I hope you can help.
I need to use binary mode, and I am trying to save a structure that
contains a couple of pointers to
dynamicaly allocated data.  Example

struct S {
        int      val1;
        float   *val2;
        int     *val3;
        int      val4;

Quote:
};

        struct S s;

        val1 = 1;
        val2 = new float[42];   // I use a couple of C++ features  : )
        val3 = new int[10];
        val4 = 9;

        // I initialize the pointers with data here.....Then

        fwrite(&s, sizeof(struct S), 1, out);

        I have tried this different ways, and I have tried just writing the
pointer:

        fwrite(&s.val2, sizeof(s.val2), 1, out);

        When I read the structure back in, val1 is ok, but val2 and val3 are
garbage!  Nothing seems to work. Can you help?



Mon, 30 Oct 2000 03:00:00 GMT  
 How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

Quote:

>I have a question, I hope you can help.
>I need to use binary mode, and I am trying to save a structure that
>contains a couple of pointers to
>dynamicaly allocated data.  Example

>struct S {
> int val1;
> float *val2;
> int *val3;

   You declared val2 and val3 as pointers.
Quote:
> int val4;
>};
> struct S s;

> val1 = 1;
> val2 = new float[42]; // I use a couple of C++ features  : )
> val3 = new int[10];
> val4 = 9;

> // I initialize the pointers with data here.....Then

> fwrite(&s, sizeof(struct S), 1, out);

   You are writing the addresses of val2 and val3, not the values.
Quote:

> I have tried this different ways, and I have tried just writing the
>pointer:

> fwrite(&s.val2, sizeof(s.val2), 1, out);

   Drop the &, it is already a pointer and use sizeof(float). sizeof(s.val2)
will give you the size of the pointer.

[deleted]



Mon, 30 Oct 2000 03:00:00 GMT  
 How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

Quote:

>    When I read the structure back in, val1 is ok, but val2 and val3 are
>garbage!  Nothing seems to work. Can you help?

Of course it doesn't work! You can't write pointers to disk and expect
them to be meaningful when you read them back. Only in certain limiting
circumstances will it work, namely if you read the pointers back
while the objects they point to still exist. Of course, this precludes
reading in the data in a subsequent run of the same program.

What you must do is write code that writes out your entire data
structure to a file in some suitable form---a file format that
suits your data. You can't use, because these are useless for
locating data within the file; you must convert your pointer fields
into integers that give locations within the file expressed
as offsets from the start of the file measured in bytes
or what have you.



Mon, 30 Oct 2000 03:00:00 GMT  
 How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

Quote:


> >       When I read the structure back in, val1 is ok, but val2 and val3 are
> >garbage!  Nothing seems to work. Can you help?

> Of course it doesn't work! You can't write pointers to disk and expect
> them to be meaningful when you read them back. Only in certain limiting
> circumstances will it work, namely if you read the pointers back
> while the objects they point to still exist. Of course, this precludes
> reading in the data in a subsequent run of the same program.

> What you must do is write code that writes out your entire data
> structure to a file in some suitable form---a file format that
> suits your data. You can't use, because these are useless for
> locating data within the file; you must convert your pointer fields
> into integers that give locations within the file expressed
> as offsets from the start of the file measured in bytes
> or what have you.

Implmenting a linked list or pointer system in a file is different than
memory based pointer systems.  The primary difference is the pointer
concept.  In memory based systems the pointers are addresses.  In file
systems, they *can* be file offsets.  So one solution is to declare the
pointers as a union of memory pointers and file offsets:

struct node
{
    char key[32];
    union
    {
        struct node * memptr;
        unsigned long file_offset;
    } next_link;

Quote:
};

The other issues you need to watch out for are the "address of" and
indirection operators, and you need to allocate a structure then read it
in before accessing it.
--
Thomas Matthews
StorageTek

voice: (303) 661-5674  fax: (303) 673-2568


Mon, 30 Oct 2000 03:00:00 GMT  
 How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

Quote:

>I have a question, I hope you can help.
>I need to use binary mode, and I am trying to save a structure that
>contains a couple of pointers to
>dynamicaly allocated data.  Example

>struct S {
>        int      val1;
>        float   *val2;
>        int     *val3;
>        int      val4;
>};
>        struct S s;

>        val1 = 1;
>        val2 = new float[42];   // I use a couple of C++ features  : )
>        val3 = new int[10];
>        val4 = 9;

>        // I initialize the pointers with data here.....Then

>        fwrite(&s, sizeof(struct S), 1, out);

>        I have tried this different ways, and I have tried just writing the
>pointer:

You've created three distinct objects here (one structure and two arrays).
so you'll need three writes to save them out.

         fwrite(&s, sizeof s, 1, out);
         fwrite(s.val2, sizeof *s.val2, 42, out);
         fwrite(s.val3, sizeof *s.val3, 10, out);

If the sizes of the arrays aren't fixed you'll have to save those out
as well, perhaps as extra members of the structure. You should always check
for failure on file I/O. fwrite() returns the number of ``array'' elements
successfully written, that wil be equal to the value of the 3rd parameter
if the write is successful. A short write indicates sone form of failure.

...

Quote:
>        When I read the structure back in, val1 is ok, but val2 and val3 are
>garbage!  Nothing seems to work. Can you help?

When you read the data back in you must create the objects to hold the data
first and set up the pointer links yourself. Pointer values stored in files
are generally meaningless, the only refer to the original objects in the
original environment that wrote the data out. To read the data back in you
might use code of the form:

         struct S s;

         fread(&s, sizeof s, 1, in);
         val2 = new float[42];   /* I'll let this pass this time! */
         val3 = new int[10];

These set the pointers in s up. val2 and val3 need to be assigned after
the initial fread() or it will just overwrite them.

         fread(s.val2, sizeof *s.val2, 42, in);
         fread(s.val3, sizeof *s.val3, 10, in);

Again, you should test for input failure. fread()'s return is the same as
fwrite()'s except that a ``short'' read may indicate end-of-file as opposed
to an error condition.

--
-----------------------------------------


-----------------------------------------



Mon, 30 Oct 2000 03:00:00 GMT  
 How Do I fwrite() and fread() A Structure that contains pointers to dynamic data


Quote:


> > fwrite(&s.val2, sizeof(s.val2), 1, out);

>    Drop the &, it is already a pointer and use sizeof(float). sizeof(s.val2)
> will give you the size of the pointer.

Another suitable way would be to use "sizeof(*s.val2)". This is a bit
more typesafe, because it will still be correct, when the type of
the pointer changes.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Tue, 31 Oct 2000 03:00:00 GMT  
 How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

Quote:

>         val2 = new float[42];   // I use a couple of C++ features  : )
>         val3 = new int[10];

Hi bsi,

If you use C++, you have contacted the wrong newsgroup. For C++ code
please ask in comp.lang.c++. Neither "new" nor "//" are part of the
standard C language.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Tue, 31 Oct 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. fwrite,fread,File & Structures

2. far pointer and fread/fwrite, possible?

3. Pointers to Structure that contains pointer to Function

4. dereferencing pointer to a structure containing a pointer to int

5. fwrite data structure clips some elements

6. reading a structure containing pointers

7. structure containing pointer to char over the network

8. self contained pointers to structures

9. Structure containing pointer problem

10. fread, fwrite probs

11. Please Help (fread, fwrite)

12. fwrite(), fread() portability.

 

 
Powered by phpBB® Forum Software