0

我正在写一个 .tga 阅读器。我可以完美地读取标题,然后我想读取文件的数据部分,但如果我到了那里,它会给我错误:

Unhandled exception at 0x76ffa2ce in TGALoader.exe 0xC0000005: Access violation reading location 0xffff0008

文件的长度是65580,我在长度标头65536之后读取18 bytes

我当前的代码是(我删掉了不重要的部分):

// Texture variables    
GLint   bitsPP;
GLsizei width;
GLsizei height;
GLubyte *imgData;
/////////////////////////////////////////////////

file.seekg( 0, std::ios::end );
std::cout << file.tellg() << "\n"; // 65580
file.seekg( 0, std::ios::beg );

file.read( ( char* )&tGAHeader, sizeof( tGAHeader ) );

texture->width  = tGAHeader[13] * 256 + tGAHeader[12];
texture->height = tGAHeader[15] * 256 + tGAHeader[14];
texture->bitsPP = tGAHeader[16];

short bytesPP = texture->bitsPP / 8; // 4
unsigned int imgSize = texture->width * texture->height * bytesPP; // 65536

texture->imgData = new GLubyte[imgSize];

file.read( ( char* )&texture->imgData, imgSize ); // Access violation reading location

我无法想象有什么问题,所以我希望有人能帮助我。

提前致谢!

4

1 回答 1

3

改变

file.read( ( char* )&texture->imgData, imgSize )

经过

file.read( ( char* )texture->imgData, imgSize )

texture->imgData只是一个指针,不要在第二个间接级别使用它

于 2012-12-12T11:58:00.073 回答