1 /* 2 获取文本文件的字符集类型 3 ANSI, Unicode, UTF-8 4 */ 5 6 #include <cstdio> 7 //#include <cstring> 8 9 #define ERROR_OPEN 0x7F 10 #define ASCII_GBK_T 1 11 #define UNICODE16_L_T 2 12 #define UNICODE16_B_T 3 13 #define UTF8_T 4 14 #define UNICODE32_L_T 5 15 #define UNICODE32_B_T 6 16 #define GB_18030_T 7 17 #define UTF1_T 8 18 #define UTF_EBCDIC_T 9 19 #define UTF7_T 10 20 #define SCSfUNICODE_T 11 21 #define BOCU1_T 12 22 23 24 short int getType(char* fname) 25 { 26 short int value = 0; 27 unsigned char fType[4] = { 0x0,0x0,0x0,0x0 }; 28 FILE *fp=fopen(fname,"rb"); 29 if(!fp) { 30 return ERROR_OPEN; 31 } 32 33 fseek(fp,0L,SEEK_SET); 34 fread(fType,3,1,fp); 35 if(fType[0]==0xFF && fType[1]==0xFE) { 36 value = UNICODE16_L_T; 37 } 38 else if(fType[0]==0xFE && fType[1]==0xFF) { 39 value = UNICODE16_B_T; 40 } 41 else if(fType[0]==0xEF && fType[1]==0xBB && fType[2]==0xBF) { 42 value = UTF8_T; 43 } 44 45 else if(fType[0]==0x00 && fType[1]==0x00 && fType[2]==0xFF && fType[3]==0xFE) { 46 value = UNICODE32_L_T; 47 } 48 else if(fType[0]==0x00 && fType[1]==0x00 && fType[2]==0xFE && fType[3]==0xFF) { 49 value = UNICODE32_B_T; 50 } 51 else if(fType[0]==84 && fType[1]==31 && fType[2]==95 && fType[3]==33) { 52 value = GB_18030_T; 53 } 54 else if(fType[0]==0xF7 && fType[1]==0x64 && fType[2]==0x4C) { 55 value = UTF1_T; 56 } 57 else if(fType[0]==0xDD && fType[1]==0x73 && fType[2]==0x66 && fType[3]==0x73) { 58 value = UTF_EBCDIC_T; 59 } 60 else if(fType[0]==0x0E && fType[1]==0xFE && fType[2]==0xFF) { 61 value = SCSfUNICODE_T; 62 } 63 else if((fType[0]==0x2B && fType[1]==0x2F && fType[2]==0x76) && (fType[3]==0x38 || fType[3]==0x39 || fType[3]==0x2B || fType[3]==0x2F )) { 64 value = UTF7_T; 65 } 66 else if(fType[0]==0xFB && fType[1]==0xEE && fType[2]==0x28) { 67 value = BOCU1_T; 68 } 69 else { 70 value = ASCII_GBK_T; 71 } 72 73 fclose(fp); 74 return value; 75 } 76 77 int main(int argc,char* argv[]) 78 { 79 char* fname; 80 short int typeIs=-1; 81 printf("Input filename: "); 82 scanf("%s",fname); 83 typeIs = getType(fname); 84 switch(typeIs) { 85 case 0: 86 printf(" Other file..\n"); 87 break; 88 case 1: 89 printf(" Ascii (GBK) file.\n"); 90 break; 91 case 2: 92 printf(" Unicode Little ( UTF-16 ) file (Windows OS use) \n"); 93 break; 94 case 3: 95 printf(" Unicode Big ( UTF-16 ) file (Linux use) \n"); 96 break; 97 case 4: 98 printf(" UTF-8 file.\n"); 99 break; 100 case 5: 101 printf(" Unicode Little ( UTF-16 ) file (Windows OS use) \n"); 102 break; 103 case 6: 104 printf(" Unicode Big ( UTF-16 ) file (Linux use) \n"); 105 break; 106 case 7: 107 printf(" GB18030 file\n"); 108 break; 109 case 8: 110 printf(" UTF-1 file\n"); 111 break; 112 case 9: 113 printf(" UTF EBCDIC file\n"); 114 break; 115 case 10: 116 printf(" UTF7 file\n"); 117 break; 118 case 11: 119 printf(" Standard Compression Scheme for Unicode file\n"); 120 break; 121 case 12: 122 printf(" BOCU-1 file\n"); 123 break; 124 default: 125 printf(" Message: file not open\n"); 126 break; 127 } 128 129 /* 130 char str[]="-kbhit"; 131 if(!strcmp(argv[1],str)) { 132 getchar(); 133 getchar(); 134 } 135 */ 136 137 /*if(argc!=1) { 138 getchar(); 139 getchar(); 140 }*/ 141 142 //getchar(); 143 //getchar(); 144 145 return 0; 146 }