Reading Binary Files

Moderator
Posts: 704
Joined: 2002.04
Post: #16
If you mean the technique from my original post where I included code, the major difference between my method and Fenris' is that with mine you have to make lots of calls to fwrite()/fread() (but you gain the advantage that you can branch the input code based on the values retrieved), whereas with Fenris' method you have to play with pointer arithmetic (but gain the advantage that you only need one call to fwrite()/fread(), but at the disadvantage that you lose the ability to branch based on values retrieved.)

There is of course no reason why you couldn't mix both methods, loading chunks of values and then inspecting the values loaded up to that point for whether to branch...

Code:
#ifdef __cplusplus
    extern "C" {
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
    };
#endif

class TestClass
{
    public:
    int m_lives;
    char *m_name;

    TestClass( void )
    {
    m_lives = 0;
    m_name = NULL;
    };

    ~TestClass( void )
    {
    if( m_name != NULL )
    {
    free( m_name );
    }
    };

    void Save( void )
    {
    FILE *file;
    if(( file = fopen( "sealfin.bin", "wb" )) == NULL )
    {
    printf( "Scream! And die!" );
    return;
    }
    fwrite( &m_lives, sizeof( int ), 1, file );
    /* Yes, I know I should probably check that the int has actually been written to the file, but I'm in a rush... */
    int length = strlen( m_name );
    fwrite( &length, sizeof( int ), 1, file );
    fwrite( m_name, sizeof( char ), length, file );
    fclose( file );
    return;
    };

    void Load( void )
    {
    FILE *file;
    int length;
    if(( file = fopen( "sealfin.bin", "rb" )) == NULL )
    {
    printf( "Scream! And die!" );
    return;
    }
    fread( &m_lives, sizeof( int ), 1, file );
    /* ...and here I should be checking that an int has actually been read from the file. */
    fread( &length, sizeof( int ), 1, file );
    m_name = ( char* )malloc( length + 1 );
    fread( m_name, sizeof( char ), length, file );
    m_name[ length ] = '\0';
    fclose( file );
    return;
    };

    void GiveName( const char *p_name )
    {
    m_name = ( char* )malloc( strlen( p_name ) + 1 );
    strcpy( m_name,
    p_name );
    return;
    };
};


int main()
{
    TestClass obj;
    obj.Load();
    printf( "\nlives: %d\n", obj.m_lives );
    printf( "\nname:  %s\n\n", obj.m_name );
    obj.m_lives = 21;
    obj.GiveName( "Mark Bishop" );
    obj.Save();
    return 0;
}

The above code is not particularly robust (i.e. calling GiveName() more than once will obviously leak memory, amongst other fun...)

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Binary Tree and Objective-c mnorton 0 3,656 Sep 7, 2009 10:44 AM
Last Post: mnorton
  Carbon - Reading / Writing Text Files dave05 10 4,582 Aug 1, 2006 05:42 PM
Last Post: dave05
  Reading/writing files in Carbon? ia3n_g 2 2,574 Jul 23, 2006 06:54 PM
Last Post: ia3n_g