Thread: Dump C struct (object) to a file and restore it back (serialize)

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    121

    Dump C struct (object) to a file and restore it back (serialize)

    Hello, I am trying to serialize a simple C struct. I am dumping it to a file then try to read and restore it. However I got wrong data. Here is my idea:
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    
    
    struct a
    {
        int i;
        char str[64];
        struct
        {
            float x;
            float y;
            float z;
        } vec3;
    
    
    };
    
    
    
    
    static struct a setA(int i, char* s, float f1, float f2, float f3)
    {
        struct a newa;
        newa.i = i;
        strcpy(newa.str, s);
        newa.vec3.x = f1;
        newa.vec3.y = f2;
        newa.vec3.z = f3;
    
    
        return newa;
    }
    
    
    
    
    
    
    
    
    static char* dumpA(struct a* pa)
    {
        union dmp {
            struct a m_a;
            char c[sizeof(struct a)];
        } ;
    
    
        union dmp d;
    
    
        d.m_a.i = pa->i;
        strcpy(d.m_a.str, pa->str);
        d.m_a.vec3.x = pa->vec3.x;
        d.m_a.vec3.y = pa->vec3.y;
        d.m_a.vec3.z = pa->vec3.z;
    
    
        int i =0;
        static char dumped[sizeof(union dmp)];
    
    
        FILE* fp = fopen("serialized", "wb");
        if (!fp) {
            return 0;
        }
    
    
        for(i=0; i < sizeof(d); i++) {
            fprintf(fp, "%0x", d.c[i]);
            dumped[i] = d.c[i];
        }
        fclose(fp);
        return &dumped;
    }
    
    
    
    
    
    
    
    
    static void printA(struct a* pa)
    {
        printf("[%d][%s][%f;%f;%f]\n", pa->i, pa->str,
               pa->vec3.x, pa->vec3.y, pa->vec3.z);
    }
    
    
    
    
    struct a* restoreA(char* fname)
    {
        return 0;
    }
    
    
    int main(int argc, char *argv[])
    {
        struct a newa = setA(1, "bla bla bla", 1.0, 2.0, 3.0);
        printA(&newa);
    
    
        char* odump = dumpA(&newa);
    
    
        FILE *fp = fopen("serialized", "r");
        char buff[256]={0};
        while(fgets(buff, 256, fp)) {
    
    
        }
    
    
        union dm {
            struct a ma;
            char c[sizeof(struct a)];
        };
    
    
        union dm d;
        memset(d.c, 0, sizeof(struct a));
        strcpy(d.c, buff);
    
    
        printf("[%d][%s][%f,%f,%f]\n", d.ma.i, d.ma.str,
               d.ma.vec3.x, d.ma.vec3.y, d.ma.vec3.z);
    
        fclose(fp);
        return 0;
    }
    When I read it back it seems filled with data but the data is bad. In first time I don`t know if it is dumped correctly.

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    121
    I`ve found a way to restore the object via the cast from the buffer:
    After reading the object to a char buffer with a sufficiet size:
    Code:
    struct a* pa = (struct a*) buff;
    Also the serialize procedure must dump the object in a %c char not in hex as I did
    Code:
    fprintf(fp, "%c", c);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you seem to be over complicating things.

    So long as you don't care about portability (say to a machine with different sized ints, or different structure padding rules), and your struct doesn't contain any pointers, then you can do this.

    Code:
    struct a foo;
    FILE *fp = fopen("file","wb");
    if ( fwrite(&foo,sizeof(foo),1,fp) == 1 ) // success
    
    ...
    FILE *fp = fopen("file","rb");
    if ( fread(&foo,sizeof(foo),1,fp) == 1 ) // success
    You can do what you did, but you need to be totally consistent between the writer and the reader.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-19-2013, 06:54 PM
  2. Core Dump when closing the ifstream object- why?
    By Krsh in forum C++ Programming
    Replies: 11
    Last Post: 11-20-2008, 06:03 AM
  3. Replies: 5
    Last Post: 06-16-2007, 11:56 PM
  4. pointer to struct object in class object
    By Stevo in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2004, 07:58 PM
  5. How to convert object file back to readable?
    By Yin in forum C++ Programming
    Replies: 5
    Last Post: 03-14-2002, 09:56 AM

Tags for this Thread