依次读出文件中的字节,如果存在'/0'则是二进制文件, 否则为ASCII文本文件!
代码实现如下:
BOOL IsASCIIFile(LPCTSTR lpFilePath)
{
char cFile[MAX_PATH] = {0};
WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, lpFilePath, -1, cFile, sizeof(cFile), NULL, NULL);
if(cFile==NULL)
{
return FALSE;
}
struct stat f_stat;
if(stat(cFile,&f_stat) == -1)
{
return FALSE;
}
long lngFilesize = 0;
lngFilesize = (long)f_stat.st_size;
if(lngFilesize <= 0)
{
return FALSE;
}
FILE *ft;
fopen_s(&ft, cFile, "rb");
if(ft==NULL)
{
return FALSE;
}
char btData[1];
long lngReadPos;
long lngMaxRead = 40960;
if(lngMaxRead > lngFilesize)
{
lngMaxRead=lngFilesize;
}
for(lngReadPos = 1;lngReadPos <= lngMaxRead;lngReadPos++)
{
fread(&btData, 1, 1, ft);
if(btData[0]=='/0')
{
fclose(ft);
return FALSE;
}
}
fclose(ft);
return TRUE;
}