Using std::ifstream to load in an array of struct data type into a std::vector

Posted by Sent1nel on Stack Overflow See other posts from Stack Overflow or by Sent1nel
Published on 2010-05-12T16:10:39Z Indexed on 2010/05/12 16:14 UTC
Read the original article Hit count: 235

Filed under:
|
|

I am working on a bitmap loader in C++ and when moving from the C style array to the std::vector I have run into an usual problem of which Google does not seem to have the answer.

8 Bit and 4 bit, bitmaps contain a colour palette. The colour palette has blue, green, red and reserved components each 1 byte in size.

 
// Colour palette     
struct BGRQuad
{
     UInt8 blue; 
     UInt8 green; 
     UInt8 red; 
     UInt8 reserved; 
};

The problem I am having is when I create a vector of the BGRQuad structure I can no longer use the ifstream read function to load data from the file directly into the BGRQuad vector.


// This code throws an assert failure!
std::vecotr quads;
if (coloursUsed) // colour table available
{   // read in the colours
    quads.reserve(coloursUsed);
    inFile.read( reinterpret_cast(&quads[0]), coloursUsed * sizeof(BGRQuad) ); 
}

Does anyone know how to read directly into the vector without having to create a C array and copy data into the BGRQuad vector?

© Stack Overflow or respective owner

Related posts about c++

Related posts about ifstream