Equivalent fread using ifstream [closed]
I want to use ifstream
to read n
blocks of data like fread
, is there a way to do implement similar functionality in C++ using ifstream
?
I tried to load the TGA file, and its header looks like:
struct TgaHeader {
char identSize;
char colorMapType;
char imageType;
unsigned short colorMapStart;
unsigned short colorMapLength;
unsigned char colorMapBits;
unsigned short xstart;
unsigned short ystart;
unsigned short width;
unsigned short height;
char bits;
char descriptor;
};
Using fread
to read a header:
TgaHeader tgaHeader;
fread( &tgaHeader, 18/* sizeof( TgaHeader )*/, 1, pFile );
Now I want to use it with ifstream
, but there are no equivalent functions available. ifstream
only offers read
function which allows reading a pointer of n
size. So how could I say reading n
headers of x
bytes using fstream
?
Thank you,
As the comments under the question say, you want basic_istream<>::read()
and to use size * count
instead of the two separate arguments of fread()
.
精彩评论