IsUTF8File

来源:互联网 发布:windows常用网络命令 编辑:程序博客网 时间:2024/05/22 03:47
  1. /*=================================== 
  2. //函数名:  IsUTF8File 
  3. //作者:    guandiqun  
  4. //日期:    2011-12-01 
  5. //功能:    判断文件是否是utf-8 
  6. //输入参数:TCHAR *strFile     
  7. //返回值:  int 
  8. -2:表示文件错误 
  9. -1:表示打开文件错误 
  10. 1:是UTF-8(有bom) 
  11. 2:是UTF-8(无bom) 
  12. 0:表示不是utf-8 
  13. //===================================*/  
  14. int IsUTF8File(TCHAR *strFile)  
  15. {  
  16.     if (strFile == NULL)  
  17.     {  
  18.         return -2;  
  19.     }  
  20.     CString filename(strFile);  
  21.     ifstream fin( strFile ,ios::binary);  
  22.     if( !fin )  
  23.     {  
  24.         return -1;  
  25.     }  
  26.     else  
  27.     {  
  28.         byte bytes[3];  
  29.         fin.read((char *)&bytes,sizeof bytes);  
  30.         if ((bytes[0] == 0x44&& bytes[1] == 0x46 && bytes[2] == 0x41))// 有bom  
  31.         {     
  32.             fin.close();  
  33.             return 1;  
  34.         }  
  35.         if( (bytes[0] == 0xEF&& bytes[1] == 0xBB && bytes[2] == 0xBF) )// 无bom  
  36.         {  
  37.             fin.close();  
  38.             return 2;  
  39.         }  
  40.           
  41.         fin.close();  
  42.         return 0;  
  43.           
  44.     }  
  45. }  

原创粉丝点击