关于c++ FILE类打开读取多种编码文件(乱码,不去不成功)的问题

C++语言 码拜 9年前 (2015-05-11) 2506次浏览 0个评论

现在我写了一个程序,能够打开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);
    }
}
引用 1 楼 zhao4zhong1 的回复:

对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 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 来读取 任意一个编码的文件 并且能完整读取显示而且不乱码。 见我上面的代码

转码…………….
引用 3 楼 bjym1987 的回复:

转码…………….

引用 3 楼 bjym1987 的回复:

转码…………….

How?

40分
楼主知不知道就算人也无法区分几个字节的原始正确编码啊!
https://www.baidu.com/link?url=rBS2b5_izc2ispm6LrJ61SBiowuMBO4zRM1RBc6lKi_vxEOukigfDOhZGeN0FXN2QlwTa8JokGZirHYclDrqXq&wd=&eqid=fc48102a0000773d00000003554c2cd6&ie=utf-8
用 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 
fgetc <stdio.h> ANSI, Win 95, Win NT 
fgetwc <stdio.h> or <wchar.h> ANSI, Win 95, Win NT 
_fgetchar <stdio.h> Win 95, Win NT 
_fgetwchar <stdio.h> or <wchar.h> Win 95, Win NT 

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version 
LIBCMT.LIB Multithread static library, retail version 
MSVCRT.LIB Import library for MSVCRT.DLL, 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 
fgetc Equivalent to getc, but implemented only as a function, rather than as a function and a macro. 
fgetwc Wide-character version of fgetc. Reads c as a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.  
_fgetchar Equivalent to fgetc( stdin ). Also equivalent to getchar, but implemented only as a function, rather than as a function and a macro. Microsoft-specific; not ANSI-compatible. 
_fgetwchar Wide-character version of _fgetchar. Reads c as a multibyte character or a wide character according to whether stream is opened in text mode or binary mode. Microsoft-specific; not ANSI-compatible. 

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 
_fgettc fgetc fgetc fgetwc 
_fgettchar fgetchar fgetchar _fgetwchar 

Example

/* FGETC.C: This program uses getc to read the first
 * 80 input characters (or until the end of input)
 * and place them into a string named buffer.
 */

#include <stdio.h>
#include <stdlib.h>

void main( void )
{
   FILE *stream;
   char buffer[81];
   int  i, ch;

   /* Open file to read line from: */
   if( (stream = fopen( “fgetc.c”, “r” )) == NULL )
      exit( 0 );

   /* Read in first 80 characters and place them in “buffer”: */
   ch = fgetc( stream );
   for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ )
   {
      buffer[i] = (char)ch;
      ch = fgetc( stream );
   }

   /* Add null to end string */
   buffer[i] = “”\0″”;
   printf( “%s\n”, buffer );
   fclose( stream );
}

Output

/* FGETC.C: This program uses getc to read the first
 * 80 input characters (or

Stream I/O Routines

See Also   fputc, getc

引用 6 楼 zhao4zhong1 的回复:

https://www.baidu.com/link?url=rBS2b5_izc2ispm6LrJ61SBiowuMBO4zRM1RBc6lKi_vxEOukigfDOhZGeN0FXN2QlwTa8JokGZirHYclDrqXq&wd=&eqid=fc48102a0000773d00000003554c2cd6&ie=utf-8

垃圾 我用的是java


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于c++ FILE类打开读取多种编码文件(乱码,不去不成功)的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!