944,218 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 13652
  • C++ RSS
Jun 9th, 2006
0

Reading a Binary File to a C++ Class

Expand Post »
Is it possible to read data from a binary file to a C++ class using istream::read? I know it can be done for structures, but I couldnt do it for a class.

[Edit]
Want code?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. class BITMAPFILEHEADER
  5. {
  6. private:
  7. char type[ 2 ]; // Magic identifier
  8. unsigned int size; // File size in bytes
  9. unsigned short int reserved1; // Reserved; always zero
  10. unsigned short int reserved2; // Reserved; always zero
  11. unsigned int offset; // Offset to image data, bytes
  12.  
  13. public:
  14. friend std::istream& operator>>(std::istream& s, BITMAPFILEHEADER& r);
  15. };
  16. std::istream& operator>>(std::istream& s, BITMAPFILEHEADER& r);

std::istream& operator>>(std::istream& s, BITMAPFILEHEADER& r)
{
#if 0
    // This does not work
    s.read( reinterpret_cast <char*>(&r), sizeof(BITMAPFILEHEADER) );
    if( s.bad() )
    {
        std::cerr << "Error reading data" << std::endl;
        exit( 0 );
    }
#endif
#if 1
     // This works
    s.read( r.type, 2 * sizeof(char) );
    if( s.bad() )
    {
        std::cerr << "Error reading data" << std::endl;
        exit( 0 );
    }

    s.read( (char*)&(r.size), sizeof(unsigned int) );
    if( s.bad() )
    {
        std::cerr << "Error reading data" << std::endl;
        exit( 0 );
    }

    s.read( (char*)&(r.reserved1), sizeof(unsigned short int) );
    if( s.bad() )
    {
        std::cerr << "Error reading data" << std::endl;
        exit( 0 );
    }

    s.read( (char*)&(r.reserved2), sizeof(unsigned short int) );
    if( s.bad() )
    {
        std::cerr << "Error reading data" << std::endl;
        exit( 0 );
    }

    s.read( (char*)&(r.offset), sizeof(unsigned int) );
    if( s.bad() )
    {
        std::cerr << "Error reading data" << std::endl;
        exit( 0 );
    }
#endif

    return s;
}
Last edited by WolfPack; Jun 9th, 2006 at 12:49 pm.
Similar Threads
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jun 9th, 2006
1

Re: Reading a Binary File to a C++ Class

I think it is better to take the serialized approach you presented. Even with a POD structure the binary read/write is inherently non-portable.

Nit - consider this:
s.read( r.type, sizeof r.type );
//s.read( r.type, 2 * sizeof(char) );

// ...

s.read( (char*)&(r.size), sizeof r.size );
//s.read( (char*)&(r.size), sizeof(unsigned int) );
And FWIW:
http://www.parashift.com/c++-faq-lit....html#faq-36.6
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 9th, 2006
0

Re: Reading a Binary File to a C++ Class

Okay. Got it. Thanks for the additional info too.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Feb 4th, 2010
-2

be careful though

I think it is better to take the serialized approach you presented. Even with a POD structure the binary read/write is inherently non-portable.

Nit - consider this:
s.read( r.type, sizeof r.type );
//s.read( r.type, 2 * sizeof(char) );

// ...

s.read( (char*)&(r.size), sizeof r.size );
//s.read( (char*)&(r.size), sizeof(unsigned int) );
And FWIW:
http://www.parashift.com/c++-faq-lit....html#faq-36.6
sometimes in reading binary files, you may want take a byte of data and store it into something bigger. Then it is NOT good to use the sizeof( <class variable> ) to specify how much of the file to read. EX. what if you want to read 1 byte of the file into an" integer size" class variable, r::int var1? using the sizeof(r.var1) in the read statement would indicate that 4 bytes are to be read instead of just 1 byte. this will throw off the parsing of the file.
Reputation Points: 32
Solved Threads: 0
Newbie Poster
bbowles2 is offline Offline
1 posts
since Feb 2010
Feb 4th, 2010
0
Re: Reading a Binary File to a C++ Class
This is your lucky day -- I gave you positive instead of negative rep for bumping a 4-year-old thread

If you only read one byte into a 4-byte integer that would make the integer not worth shit. You can't do it like that (at least so that it will produce a valid integer). First read the byte into a char variable, then typecast the char into the integer. The sizeof operator will still work because sizeof(char) = 1.
Last edited by Ancient Dragon; Feb 4th, 2010 at 12:30 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,966 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem in C++ Task: Please Help
Next Thread in C++ Forum Timeline: Help With the Visual C++ Debugger





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC