fopen,fread和1A
//=====================================================================================================
//TITLE:
// fopen,fread和1A
//AUTHOR:
// norains
//DATE:
// Friday 25-July-2006
//=====================================================================================================
如果fopen不采用二进制方式打开,如:fopen(filename,"r"),只要文件中有一字节为1A,fread函数则停止读取.如果采用fopen(filename,"rb")则不会出现此问题.
可以采用下段代码读取任意的png图片来验证,如果fopen采用"r"方式,则fread只能读取5个字节,因为png格式的图片第六位为1A!
代码如下(在EVC下用_wfopen代替fopen):
int GetReadNum(LPCTSTR pcszFileName)
{
#define MAX_READ_NUM 10
FILE *pfs;
pfs =_wfopen(pcszFileName,TEXT("r") ); //pfs =_wfopen(pcszFileName,TEXT("rb") );
if(pfs==NULL)
{
return 0;
}
int iType=IMG_NOSUPPORT;
PBYTE pBuffer=NULL;
pBuffer=(PBYTE)malloc(sizeof(BYTE)*MAX_READ_NUM);
int iReadNum=fread(pBuffer,sizeof(BYTE),MAX_READ_NUM,pfs);
free(pbuffer);
return iReadNum;
}
用此代码读取png图片,返回值将是5!如果fopen是"rb",则一切正常,为10!
//TITLE:
// fopen,fread和1A
//AUTHOR:
// norains
//DATE:
// Friday 25-July-2006
//=====================================================================================================
如果fopen不采用二进制方式打开,如:fopen(filename,"r"),只要文件中有一字节为1A,fread函数则停止读取.如果采用fopen(filename,"rb")则不会出现此问题.
可以采用下段代码读取任意的png图片来验证,如果fopen采用"r"方式,则fread只能读取5个字节,因为png格式的图片第六位为1A!
代码如下(在EVC下用_wfopen代替fopen):
int GetReadNum(LPCTSTR pcszFileName)
{
#define MAX_READ_NUM 10
FILE *pfs;
pfs =_wfopen(pcszFileName,TEXT("r") ); //pfs =_wfopen(pcszFileName,TEXT("rb") );
if(pfs==NULL)
{
return 0;
}
int iType=IMG_NOSUPPORT;
PBYTE pBuffer=NULL;
pBuffer=(PBYTE)malloc(sizeof(BYTE)*MAX_READ_NUM);
int iReadNum=fread(pBuffer,sizeof(BYTE),MAX_READ_NUM,pfs);
free(pbuffer);
return iReadNum;
}
用此代码读取png图片,返回值将是5!如果fopen是"rb",则一切正常,为10!