C++判断文件编码格式

ANSI:        无格式定义;
Unicode:       前两个字节为FFFE;
Unicode big endian: 前两字节为FEFF; 
UTF-8:        前两字节为EFBB;

#include 
using namespace std;
 
int main()
{
    ifstream fin("C:/Users/Administrator/Desktop/w/1.cpp",ios::binary);
    unsigned char  s2;
    fin.read((char*)&s2, sizeof(s2));//读取第一个字节,然后左移8位
    int p = s2<<8;
    fin.read((char*)&s2, sizeof(s2));//读取第二个字节
    p |=s2;
 
    string code;
 
    switch(p)//判断文本前两个字节
    {
    case 0xfffe:  //65534
        code = "Unicode";    
        break;
    case 0xfeff://65279
        code = "Unicode big endian";
        break;
    case 0xefbb://61371
        code = "UTF-8";     
        break;
 default: 
        code = "ANSI"; 
     }
     fin.close(); 
     return 0;
}

你可能感兴趣的:(C++判断文件编码格式)