Reading a Binary File to a C++ Class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Reading a Binary File to a C++ Class

 
0
  #1
Jun 9th, 2006
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?

  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.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
1
  #2
Jun 9th, 2006
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
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

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

 
0
  #3
Jun 9th, 2006
Okay. Got it. Thanks for the additional info too.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC