windows自带记事本导致文本文件(UTF-8编码)起初八个字符乱码难题

tsinghua press
mp18
evaluating method for the double image
jiaoyuxvshi
balancing mechanism
hthr
amplification
bionic optimization algorithm
a r l
tcb

windows自带记事本导致文本文件(UTF-8编码)开首几个字符乱码难点,文本文件utf-8

转自:

在windows平台下,使用系统的记事本以UTF-8编码格式存款和储蓄了二个文本文件,可是出于Microsoft开发记事本的协会利用了3个特出怪异的作为来保存UTF-8编码的文书,它们布鼓雷门地在种种文件开端添加了0xefbbbf(十六进制)的字符,所以大家就会赶上不少玄而又玄的标题,比如,网页第②行只怕会显得一个“?”,明明正确的主次一编写翻译就报出语法错误,等等。

上边为一段测试程序,由记事本编辑的公文文件导致文件发轫前四个字符乱码。

图片 1

#include <stdio.h>
#define MAX_LEN 100

int main()
{
    char strLine[MAX_LEN];
    int i = 0;
    memset(strLine,0x0,MAX_LEN);

    FILE * fp;
    fp = fopen("eng_query.txt","r");
    if(NULL == fp)
    {
        printf("open file fail.\n");
        return -1;
    }

    while(fgets(strLine,MAX_LEN,fp))
    {
        printf("str = %s",strLine);//将每行字符串打印出来
        for(i=0; i<strlen(strLine); i++)//将每各字符串中的字符以十六进制格式打印出来
        {
            printf("%x  ",strLine[i]);
        }
        printf("\n\n");
    }
    fclose(fp);
    return 0;
}

图片 2

 

输入文件:

图片 3

tsinghua press
mp18
evaluating method for the double image
jiaoyuxvshi
balancing mechanism
hthr
amplification
bionic optimization algorithm
a r l
tcb

图片 4

 

出口文件:

图片 5

str = 锘縯singhua press
ffffffef  ffffffbb  ffffffbf  74  73  69  6e  67  68  75  61  20  70  72  65  73
  73  a

str = mp18
6d  70  31  38  a

str = evaluating method for the double image
65  76  61  6c  75  61  74  69  6e  67  20  6d  65  74  68  6f  64  20  66  6f
72  20  74  68  65  20  64  6f  75  62  6c  65  20  69  6d  61  67  65  a

str = jiaoyuxvshi
6a  69  61  6f  79  75  78  76  73  68  69  a

str = balancing mechanism
62  61  6c  61  6e  63  69  6e  67  20  6d  65  63  68  61  6e  69  73  6d  a

str = hthr
68  74  68  72  a

str = amplification
61  6d  70  6c  69  66  69  63  61  74  69  6f  6e  a

str = bionic optimization algorithm
62  69  6f  6e  69  63  20  6f  70  74  69  6d  69  7a  61  74  69  6f  6e  20
61  6c  67  6f  72  69  74  68  6d  a

str = a r l
61  20  72  20  6c  a

str = tcb
74  63  62  a

图片 6

 

小编们能够发现文件初始多少个字符乱码,每一种汉字占用五个字符,因而第五个字符本来为‘t’,也不得不乱码凸显出来。

之所以,能够选取Notepad++代替记事本。必要将Notepad++的暗中同意编码设置为UTF-8
without BOM即可;

图片 7

转自:
在windows平台下…

#include <stdio.h>
#define MAX_LEN 100

int main()
{
    char strLine[MAX_LEN];
    int i = 0;
    memset(strLine,0x0,MAX_LEN);

    FILE * fp;
    fp = fopen("eng_query.txt","r");
    if(NULL == fp)
    {
        printf("open file fail.\n");
        return -1;
    }

    while(fgets(strLine,MAX_LEN,fp))
    {
        printf("str = %s",strLine);//将每行字符串打印出来
        for(i=0; i<strlen(strLine); i++)//将每各字符串中的字符以十六进制格式打印出来
        {
            printf("%x  ",strLine[i]);
        }
        printf("\n\n");
    }
    fclose(fp);
    return 0;
}
str = 锘縯singhua press
ffffffef  ffffffbb  ffffffbf  74  73  69  6e  67  68  75  61  20  70  72  65  73
  73  a

str = mp18
6d  70  31  38  a

str = evaluating method for the double image
65  76  61  6c  75  61  74  69  6e  67  20  6d  65  74  68  6f  64  20  66  6f
72  20  74  68  65  20  64  6f  75  62  6c  65  20  69  6d  61  67  65  a

str = jiaoyuxvshi
6a  69  61  6f  79  75  78  76  73  68  69  a

str = balancing mechanism
62  61  6c  61  6e  63  69  6e  67  20  6d  65  63  68  61  6e  69  73  6d  a

str = hthr
68  74  68  72  a

str = amplification
61  6d  70  6c  69  66  69  63  61  74  69  6f  6e  a

str = bionic optimization algorithm
62  69  6f  6e  69  63  20  6f  70  74  69  6d  69  7a  61  74  69  6f  6e  20
61  6c  67  6f  72  69  74  68  6d  a

str = a r l
61  20  72  20  6c  a

str = tcb
74  63  62  a
str = 锘縯singhua press
ffffffef  ffffffbb  ffffffbf  74  73  69  6e  67  68  75  61  20  70  72  65  73
  73  a

str = mp18
6d  70  31  38  a

str = evaluating method for the double image
65  76  61  6c  75  61  74  69  6e  67  20  6d  65  74  68  6f  64  20  66  6f
72  20  74  68  65  20  64  6f  75  62  6c  65  20  69  6d  61  67  65  a

str = jiaoyuxvshi
6a  69  61  6f  79  75  78  76  73  68  69  a

str = balancing mechanism
62  61  6c  61  6e  63  69  6e  67  20  6d  65  63  68  61  6e  69  73  6d  a

str = hthr
68  74  68  72  a

str = amplification
61  6d  70  6c  69  66  69  63  61  74  69  6f  6e  a

str = bionic optimization algorithm
62  69  6f  6e  69  63  20  6f  70  74  69  6d  69  7a  61  74  69  6f  6e  20
61  6c  67  6f  72  69  74  68  6d  a

str = a r l
61  20  72  20  6c  a

str = tcb
74  63  62  a

转自:

输出文件:

 

上面为一段测试程序,由记事本编辑的文书文件导致文件开端前四个字符乱码。

咱们得以窥见文件开首四个字符乱码,各样汉字占用四个字符,因而第伍个字符本来为‘t’,也不得不乱码展现出来。

输入文件:

str = 锘縯singhua press
ffffffef  ffffffbb  ffffffbf  74  73  69  6e  67  68  75  61  20  70  72  65  73
  73  a

str = mp18
6d  70  31  38  a

str = evaluating method for the double image
65  76  61  6c  75  61  74  69  6e  67  20  6d  65  74  68  6f  64  20  66  6f
72  20  74  68  65  20  64  6f  75  62  6c  65  20  69  6d  61  67  65  a

str = jiaoyuxvshi
6a  69  61  6f  79  75  78  76  73  68  69  a

str = balancing mechanism
62  61  6c  61  6e  63  69  6e  67  20  6d  65  63  68  61  6e  69  73  6d  a

str = hthr
68  74  68  72  a

str = amplification
61  6d  70  6c  69  66  69  63  61  74  69  6f  6e  a

str = bionic optimization algorithm
62  69  6f  6e  69  63  20  6f  70  74  69  6d  69  7a  61  74  69  6f  6e  20
61  6c  67  6f  72  69  74  68  6d  a

str = a r l
61  20  72  20  6c  a

str = tcb
74  63  62  a

上面为一段测试程序,由记事本编辑的文件文件导致文件开首前四个字符乱码。

图片 8

图片 9

据此,能够选拔Notepad++代替记事本。须要将Notepad++的暗中认可编码设置为UTF-8
without BOM即可;

发表评论

电子邮件地址不会被公开。 必填项已用*标注