gpt4 book ai didi

c++ - fread() 或 fseek() 在 win32 上失败但在 mac32 上失败

转载 作者:行者123 更新时间:2023-11-27 23:19:12 24 4
gpt4 key购买 nike

如果在 mac32 上运行,它运行良好;如果我在 win32 上运行它,我将在像素 139 处得到一个意外的文件结尾,如果我注释掉 fseek(在我的测试 .tga 图像中它只会寻找 0 个字节,所以应该不会有什么不同)我得到文件在第 35 像素意外结束。不过,标题读取完全正常;这只是数据。

对于不同的编译器/架构,这 2 个函数的实现方式是否不同?

struct TGAHeader
{
char idlength;
char colourmaptype;
char datatypecode;
short colourmaporigin;
short colourmaplength;
char colourmapdepth;
short x_origin;
short y_origin;
short width;
short height;
char bitsperpixel;
char imagedescriptor;
};

bool read()
{
printf("Reading TGA file...\n");

header.idlength = fgetc(TGAFile);
header.colourmaptype = fgetc(TGAFile);
header.datatypecode = fgetc(TGAFile);
fread(&header.colourmaporigin,2,1,TGAFile);
fread(&header.colourmaplength,2,1,TGAFile);
header.colourmapdepth = fgetc(TGAFile);
fread(&header.x_origin,2,1,TGAFile);
fread(&header.y_origin,2,1,TGAFile);
fread(&header.width,2,1,TGAFile);
fread(&header.height,2,1,TGAFile);
header.bitsperpixel = fgetc(TGAFile);
header.imagedescriptor = fgetc(TGAFile);

printf("...TGA file has been read\n\n");
printf("ID length: %d\n",header.idlength);
printf("Colourmap type: %d\n",header.colourmaptype);
printf("Image type: %d\n",header.datatypecode);
printf("Colour map offset: %d\n",header.colourmaporigin);
printf("Colour map length: %d\n",header.colourmaplength);
printf("Colour map depth: %d\n",header.colourmapdepth);
printf("X origin: %d\n",header.x_origin);
printf("Y origin: %d\n",header.y_origin);
printf("Width: %d\n",header.width);
printf("Height: %d\n",header.height);
printf("Bits per pixel: %d\n",header.bitsperpixel);
printf("Descriptor: %d\n",header.imagedescriptor);

bytesToRead = header.bitsperpixel / 8;

if(!errCheck())
{
m_bLoaded = false;
return false;
}
//set stream position indicator to the start of the data
fseek(TGAFile,(header.idlength+(header.colourmaptype * header.colourmaplength)),SEEK_CUR);

//allocate space to store data
releaseTGAData();
pixelData = new unsigned char*[header.width*header.height];
for(int i=0;i<header.width*header.height;i++)
pixelData[i] = new unsigned char[bytesToRead];

for(int i=0;i<header.width * header.height;i++)
{
if (fread(pixelData[i],1,bytesToRead,TGAFile) != bytesToRead)
{
printf("Unexpected end of file at pixel %d\n",i);
m_bLoaded = false;
releaseTGAData();
return false;
}
}
fclose(TGAFile);

return true;
}

编辑:这是加载函数

bool load(const char* inFilename = NULL)
{
m_bLoaded = false;
if(NULL!=filename)
delete [] filename;
filename = new char[128];

//if the filename has not been selected by the programmer
if(NULL==inFilename)
{
//ask the user for it
printf("please enter the filename of an uncompressed TGA file:\n");
scanf("%s", filename);
}
else sprintf(filename, inFilename);

if ((TGAFile = fopen(filename,"r")) == NULL)
{
printf("File open failed, please try again\n");
return false;
}
if(!read())
return false;

m_bLoaded=true;
return true;
}

最佳答案

问题是您以文本模式 打开文件。在 Windows 上,输入函数随后会将字节序列 0x0d 0x0a(Windows 换行符序列"\r\n")转换为单个字符。

Windows 很可能还会检查字节 0x19(我认为),它是 CTRL-Z 并且是结束- Windows 中的文件字符。

关于c++ - fread() 或 fseek() 在 win32 上失败但在 mac32 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14551353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com