开发者

Equivalent fread using ifstream [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_Python百科 Closed 11 years ago.

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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜