现在我写了一个程序,能够打开ASCI文件 .c 文件 .h文件,但是打开utf-8和unicode文件时就会乱码!(而且只显示读取的少部分乱码),有什么方法 可以让我打开任意编码方式的文本文件 或者大部分编码文本(常见的)????? void ReadFile(FILE *fp) { int CurPos, FileSize; CurPos = ftell(fp); fseek(fp, 0L, SEEK_END); FileSize = ftell(fp); fseek(fp, CurPos, SEEK_SET); while(BufferSize < FileSize) InFlate(); int i = 0; while((*(Str + i++) = getc(fp)) != EOF) ; Length = i-1; SetLine(); } |
|
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A
void HexDump(char *buf,int len,int addr) { int i,j,k; char binstr[80]; for (i=0;i<len;i++) { if (0==(i%16)) { sprintf(binstr,"%08x -",i+addr); sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]); } else if (15==(i%16)) { sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]); sprintf(binstr,"%s ",binstr); for (j=i-15;j<=i;j++) { sprintf(binstr,"%s%c",binstr,(""!""<buf[j]&&buf[j]<=""~"")?buf[j]:"".""); } printf("%s\n",binstr); } else { sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]); } } if (0!=(i%16)) { k=16-(i%16); for (j=0;j<k;j++) { sprintf(binstr,"%s ",binstr); } sprintf(binstr,"%s ",binstr); k=16-k; for (j=i-k;j<i;j++) { sprintf(binstr,"%s%c",binstr,(""!""<buf[j]&&buf[j]<=""~"")?buf[j]:"".""); } printf("%s\n",binstr); } } |
|
我并不是要简单的解析十六进制让它显示出来,而是 我希望用一个缓存指针*Str 来读取 任意一个编码的文件 并且能完整读取显示而且不乱码。 见我上面的代码 |
|
转码…………….
|
|
How? |
|
40分 |
楼主知不知道就算人也无法区分几个字节的原始正确编码啊!
|
用 VC2005 以及之后的版本的话可以在 fopen 的时候用 ccs=XXXX 来指定编码
|
|
自顶!
while((*(Str + i++) = getc(fp)) != EOF) ; 我这里主要是想通过用FILE类的gets获取文件流 ,然后把它赋值给 char* Str的缓存, 对于ASCI编码文件 和一般txt文件 读取是没问题的 ,就是别的编码 列入UTF-8 unicode编码的文件读取怎么解决???? |
|
fgetc, fgetwc, _fgetchar, _fgetwchar
Read a character from a stream (fgetc, fgetwc) or stdin (_fgetchar, _fgetwchar). int fgetc( FILE *stream ); wint_t fgetwc( FILE *stream ); int _fgetchar( void ); wint_t _fgetwchar( void ); Function Required Header Compatibility For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version Return Value fgetc and _fgetchar return the character read as an int or return EOF to indicate an error or end of file. fgetwc and _fgetwchar return, as a wint_t, the wide character that corresponds to the character read or return WEOF to indicate an error or end of file. For all four functions, use feof or ferror to distinguish between an error and an end-of-file condition. For fgetc and fgetwc, if a read error occurs, the error indicator for the stream is set. Parameter stream Pointer to FILE structure Remarks Each of these functions reads a single character from the current position of a file; in the case of fgetc and fgetwc, this is the file associated with stream. The function then increments the associated file pointer (if defined) to point to the next character. If the stream is at end of file, the end-of-file indicator for the stream is set. Routine-specific remarks follow. Routine Remarks For more information about processing wide characters and multibyte characters in text and binary modes, see Unicode Stream I/O in Text and Binary Modes. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined Example /* FGETC.C: This program uses getc to read the first #include <stdio.h> void main( void ) /* Open file to read line from: */ /* Read in first 80 characters and place them in “buffer”: */ /* Add null to end string */ Output /* FGETC.C: This program uses getc to read the first Stream I/O Routines See Also fputc, getc |
|
垃圾 我用的是java |