saving structure with fwrite

This is a discussion on saving structure with fwrite within the C Programming forums, part of the General Programming Boards category; Hi! Let's say I have a this structure: Code: typedef struct _LEVEL_DATA { char Chars[80][20]; unsigned short Colors[80][20]; } LEVEL_DATA; ...

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

    Question saving structure with fwrite

    Hi!

    Let's say I have a this structure:
    Code:
    typedef struct _LEVEL_DATA
    {
        char Chars[80][20];
        unsigned short Colors[80][20];
    } LEVEL_DATA;
    How can I write this structure to a file using fwrite and how to read this structure from a file using fread?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    something like...

    fwrite((const void*)&LEVEL_DATA,sizeof(LEVEL_DATA),1,MyOutputFil e);

    fread((void*)&LEVEL_DATA,sizeof(LEVEL_DATA),1,MyIn putFile);

    You should check the return value from both to check it was written correctly and read correctly. both return a size_t. number of items read and number written.
    Last edited by Stoned_Coder; 02-26-2003 at 09:05 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    lol c board. sorry
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I'm not really adding much new, except maybe a few links; this is mostly packaging.

    More information may be found here. Consider using text files if you want to make it portable.
    Code:
    #include <stdio.h>
    
    typedef struct _LEVEL_DATA
    {
        char Chars[80][20];
        unsigned short Colors[80][20];
    } LEVEL_DATA;
    
    int main(void)
    {
        const char filename[] = "level.dat";
        FILE *file = fopen(filename, "wb");
        if ( file != NULL )
        {
            LEVEL_DATA tofile = {{"hi"}};
            if ( fwrite(&tofile, sizeof(tofile), 1, file) < 1 )
            {
                puts("short count on fwrite");
            }
            fclose(file);
        }
        file = fopen(filename, "rb");
        if ( file != NULL )
        {
            LEVEL_DATA fromfile = {0};
            if ( fread(&fromfile, sizeof(fromfile), 1, file) < 1 )
            {
                puts("short count on fread");
            }
            fclose(file);
            printf("fromfile.Chars[0] = \"%s\"\n", fromfile.Chars[0]);
        }
        return 0;
    }
    
    /* my output
    fromfile.Chars[0] = "hi"
    */
    Also note that _LEVEL_DATA is a reserved identifier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. fread fwrite structure
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-13-2005, 10:58 PM
  3. Saving structure to a file
    By eth0 in forum C++ Programming
    Replies: 20
    Last Post: 01-07-2004, 03:26 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

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