Thread: DataPackage class isn't working like I thought it would...

  1. #1
    Registered User
    Join Date
    Nov 2005

    DataPackage class isn't working like I thought it would...

    Okay, I have the following class cDataPackage. It basically is just a basic class for Creating/Saving/Loading dynamic resources.
    class cDataPackage
        // Data buffer and size
        void *m_Buf;
        unsigned long m_Size;
        cDataPackage() { m_Buf = NULL; m_Size = 0; }
        ~cDataPackage() { Free(); }
        void *Create(unsigned long Size)
            // Free a previously created buffer
            // Allocate some memory and return a pointer
            return (m_Buf = (void*)new char[(m_Size = Size)]);
        // Free the allocated memory
        void Free() { delete m_Buf; m_Buf = NULL; m_Size = 0; }
        BOOL Save(char *Filename)
            FILE *fp;
            // Make sure there’s something to write
            if(m_Buf != NULL && m_Size) {
                // Open file, write size and data
                if((fp=fopen(Filename, “wb”)) != NULL) {
                    fwrite(&m_Size, 1, 4, fp);
                    fwrite(m_Buf, 1, m_Size, fp);
                    return TRUE;
            return FALSE;
        void *Load(char *Filename, unsigned long *Size)
            FILE *fp;
            // Free a prior buffer
            if((fp=fopen(Filename, “rb”))!=NULL) {
                // Read in size and data
                fread(&m_Size, 1, 4, fp);
                if((m_Buf = (void*)new char[m_Size]) != NULL)
                    fread(m_Buf, 1, m_Size, fp);
                // Store size to return
                if(Size != NULL)
                    *Size = m_Size;
                // return pointer
                return m_Buf;
            return NULL;
    He is a working example
    typedef struct {
        char Name[32];
    } sName;
    int main()
        cDataPackage DP;
        DWORD Size;
        // Create the data package (w/64 bytes) and get the
        // pointer, casting it to an sName structure type.
        sName *Names = (sName*)DP.Create(64);
        // Since there are 64 bytes total, and each name uses
        // 32 bytes, then I can have 2 names stored.
        strcpy(Names[0].Name, "Jim");
        strcpy(Names[1].Name, "Adams");
        // Save the names to disk and free the data buffer
        // Load the names from disk. Size will equal 64
        // when the load function returns.
        Names = (sName*)DP.Load("names.dat", &Size);
        // Display the names
        MessageBox(NULL, Names[0].Name, "1st Name", MB_OK);
        MessageBox(NULL, Names[1].Name, "2nd Name", MB_OK);
        // Free up the data package
    That example works flawlessly. Though if I try making a struct with more data. The pointer doesn't save right. I was wondering if anyone knew how to fix this, or a suggestion to keep the basic functionality the same. Thank you so much for any help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    I don't know what you mean by "making a struct with more data. The pointer doesn't save right." Do you mean that the data isn't written entirely or correctly to the file? Or what?

    My hat is going to take a wild guess and say that you're making a struct with multiple members, trying to count the number of bytes yourself instead of using sizeof, and not getting it right due to padding. But that's just a guess.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    the problem with this approach is (among other things) it won't work for structures containing pointers and thus many non-pod types (ie: std:string, etc). I would recommend storing data in textual format, which is much easier to work with, more portable, etc. otherwise, consider using something such as the boost serialization library.
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  4. #4
    Registered User
    Join Date
    Nov 2005
    I guess I will use XML. Thank you for the info.

    Also, I was talking about adding more members to the struct. I do use sizeof for anything that requires size of data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-10-2007, 03:51 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Class not working!!! HELP
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 02-16-2006, 06:47 AM
  4. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 06:48 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM