| | |
Reading a Binary File to a C++ Class
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Is it possible to read data from a binary file to a C++ class using
[Edit]
Want code?
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)
#include <iostream> #include <fstream> class BITMAPFILEHEADER { private: char type[ 2 ]; // Magic identifier unsigned int size; // File size in bytes unsigned short int reserved1; // Reserved; always zero unsigned short int reserved2; // Reserved; always zero unsigned int offset; // Offset to image data, bytes public: friend std::istream& operator>>(std::istream& s, BITMAPFILEHEADER& r); }; 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.
バルサミコ酢やっぱいらへんで
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:
And FWIW:
http://www.parashift.com/c++-faq-lit....html#faq-36.6
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) );
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
![]() |
Similar Threads
- Reading a Binary file using Visual Basic (Visual Basic 4 / 5 / 6)
- Reading an input file as a class memeber function (C++)
- Error reading in file in binary mode (C++)
- How to load binary content of a .class file? (Java)
Other Threads in the C++ Forum
- Previous Thread: connect four program wont display anything
- Next Thread: hellllpppp
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






