How To Save Dynamic Data

This is a discussion on How To Save Dynamic Data within the C Programming forums, part of the General Programming Boards category; Hi! I have a big problem saving my dynamic data into a binary file. This is my code: Code: // ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question How To Save Dynamic Data

    Hi!

    I have a big problem saving my dynamic data into a binary file.

    This is my code:
    Code:
    // font structure
    typedef struct _FONT_DATA
    {
    	short Length, Height;
    	char Author[21], Email[31], Date[81], ID_CHAR[21], ID_FONT[11];
    	BYTE **Char;
    } FONT_DATA;
    
    FONT_DATA *FontData;
    
    .
    .
    .
    
    // this is the way I use to save my font data, for which I think is wrong
    
    fwrite (FontData, sizeof (FONT_DATA), 1, FNTFile);
    The result is that the only thing that gets saved is that static data (author, length, height, email, ...), the dynamic data is not saved at all. What to do??
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    fwrite performs a bitwise copy of a structure, this includes padding and pointer addresses, but it doesn't dereference pointers to get at the real data. The end result is that you have a worthless address in your file and your data is lost.

    >What to do??
    You have to work harder. For example, write the size in bytes of the dynamic data so that you know how much to read back later and then save the data that your pointers point to. You can't use a single fwrite, but you should avoid fread/fwrite anyway if you intend to write files with any portability at all. Unless you really really need the advantages of binary files, you would be better off writing each data member to a text file in a predefined format to ease reading. Otherwise you will still have to write each data member manually, only with proper binary operations you will end up doing more work than with text files.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-15-2009, 10:35 AM
  2. Save data from two struct arrays in one .dat file
    By IndioDoido in forum C Programming
    Replies: 5
    Last Post: 03-27-2008, 03:50 PM
  3. Global acess to dynamic allocated data
    By dereach in forum C Programming
    Replies: 2
    Last Post: 02-25-2008, 11:10 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 08:09 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21