简体   繁体   English

printf()/ fprintf()在Linux,UTF-8编码中出现问题

[英]printf()/fprintf() issues in Linux, UTF-8 encoding

So i'm running this code: 所以我正在运行这段代码:

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

int main()
{
    system("chcp 1252 > nul"); // makes system accept latin characters
    int i = 0;
    for(i = 0; i < 256; i++){
        printf("%i:\t%c\n", i, (char)i);
    }
    return 0;
}

This printed to console window all characters from extended ascii table. 这打印到控制台窗口所有字符都来自扩展的ascii表。

I now am using linux and hoped to achieve the same result. 我现在正在使用linux并希望获得相同的结果。 I am aware that linux does not use extended ASCII table. 我知道linux不使用扩展的ASCII表。 Thus i have made sure the file is encoded to UTF-8, which have worked for me before. 因此,我确保文件编码为UTF-8,这对我来说很有用。 I am using code::blocks runing console applications to gnome terminal, also set to UTF-8. 我使用code :: blocks运行控制台应用程序到gnome终端,也设置为UTF-8。 But my output is not what i expected: 但我的输出不是我的预期:

33: !
34: "
35: #
36: $
37: %
38: &
39: '
40: (
41: )
42: *
43: +
...
69: E
70: F
71: G
72: H
73: I
...
103:    g
104:    h
105:    i
106:    j
107:    k
108:    l
...
127:    
128:    �
129:    �
...    
254:    �
255:    �

What am I missing here.. there has to be a way to do it. 我在这里想念的是什么......必须有办法做到这一点。 I have tried many solutions so far, one of them being: 到目前为止,我尝试了很多解决方案,其中一个是:

...
#include <locale.h>

int main()
{
    setlocale(LC_ALL,"portuguese");
    ...
}

but so far, this has been to no avail. 但到目前为止,这一直无济于事。 Any help is appreciated 任何帮助表示赞赏

Edit 1: Ok! 编辑1:好的! I got to print UTF-8 encoded chars to terminal. 我得把UTF-8编码的字符打印到终端。 But printing to file is not working like i expected. 但是打印到文件并不像我预期的那样工作。 Using wchar.h and locale.h as such: 使用wchar.h和locale.h如下:

#include <locale.h>
#include <wchar.h>

int main(){
    setlocale(LC_ALL,"");

    wint_t index = 0;

    FILE* fpinout = fopen("UTF-8.txt","w");
    for(index = 0; index < 0x200; index++){
        printf("%i:\t%lc\n", index, index); //works fine, prints utf-8 chars to terminal
        fprintf(fpinout,"%i\t%lc", index, index); //does not work, output is wierd
    }
    fclose(fpinout);
}

I tried to use index there both as wint_t and wchar_t. 我试着在那里使用索引作为wint_t和wchar_t。 My UTF-8.txt file looks like this: 我的UTF-8.txt文件如下所示:

र㄀ĉल㌂̉ऴ㔄ԉश㜆܉स㤈उ〱ਉㄱଉ㈱ఉ㌱ഉ㐱ฉ㔱༉㘱ဉ㜱ᄉ㠱ሉ㤱ጉ〲ᐉㄲᔉ㈲ᘉ㌲ᜉ㐲᠉㔲ᤉ㘲ᨉ㜲ᬉ㠲ᰉ㤲ᴉ〳ḉㄳἉ㈳ ㌳℉㐳∉㔳⌉㘳␉㜳
┉㠳☉㤳✉〴⠉ㄴ⤉㈴⨉㌴⬉㐴Ⰹ㔴ⴉ㘴⸉㜴⼉㠴〉㤴ㄉ〵㈉ㄵ㌉㈵㐉㌵㔉㐵㘉㔵㜉㘵㠉㜵㤉㠵㨉㤵㬉〶㰉ㄶ㴉㈶㸉㌶㼉㐶䀉㔶䄉㘶䈉
㜶䌉㠶䐉㤶䔉〷䘉ㄷ䜉㈷䠉㌷䤉㐷䨉㔷䬉㘷䰉㜷䴉㠷三㤷伉〸倉ㄸ儉㈸刉㌸匉㐸吉㔸唉㘸嘉㜸圉㠸堉㤸変〹娉ㄹ嬉㈹尉㌹崉㐹帉
㔹弉㘹怉㜹愉㠹戉㤹按〱रㅤ㄰攉〱लㅦ㌰有〱ऴㅨ㔰椉〱शㅪ㜰欉〱सㅬ㤰洉ㄱरㅮㄱ漉ㄱलㅰ㌱焉ㄱऴㅲ㔱猉ㄱशㅴ㜱甉ㄱसㅶ㤱眉
㈱रㅸㄲ礉㈱लㅺ㌲笉㈱ऴㅼ㔲紉㈱शㅾ㜲缉㈱स胂㈱ह臂㌱र苂㌱ऱ菂㌱ल蓂㌱ळ藂㌱ऴ蛂㌱व蟂㌱श裂㌱ष观㌱स諂㌱ह诂㐱र賂㐱ऱ跂㐱ल軂㐱
ळ迂㐱ऴ郂㐱व釂㐱श鋂㐱ष鏂㐱स铂㐱ह闂㔱र雂㔱ऱ韂㔱ल飂㔱ळ駂㔱ऴ髂㔱व鯂㔱श鳂㔱ष鷂㔱स黂㔱ह鿂㘱रꃂ㘱ऱꇂ㘱लꋂ㘱ळꏂ㘱ऴ꓂
㘱वꗂ㘱शꛂ㘱षꟂ㘱सꣂ㘱ह꧂㜱रꫂ㜱ऱꯂ㜱ल곂㜱ळ귂㜱ऴ껂㜱व꿂㜱श냂㜱ष뇂㜱स닂㜱ह돂㠱र듂㠱ऱ뗂㠱ल뛂㠱ळ럂㠱ऴ룂㠱व맂㠱श뫂
㠱ष믂㠱स볂㠱ह뷂㤱र뻂㤱ऱ뿂㤱ल胃㤱ळ臃㤱ऴ苃㤱व菃㤱श蓃㤱ष藃㤱स蛃㤱ह蟃〲र裃〲ऱ觃〲ल諃〲ळ诃〲ऴ賃〲व跃〲श軃〲ष迃〲स郃〲ह
釃ㄲर鋃ㄲऱ鏃ㄲल铃ㄲळ闃ㄲऴ雃ㄲव韃ㄲश飃ㄲष駃ㄲस髃ㄲह鯃㈲र鳃㈲ऱ鷃㈲ल黃㈲ळ鿃㈲ऴꃃ㈲वꇃ㈲शꋃ㈲षꏃ㈲स꓃㈲हꗃ㌲रꛃ㌲ऱꟃ㌲
लꣃ㌲ळ꧃㌲ऴ꫃㌲वꯃ㌲श곃㌲ष귃㌲स껃㌲ह꿃㐲र냃㐲ऱ뇃㐲ल닃㐲ळ돃㐲ऴ듃㐲व뗃㐲श뛃㐲ष럃㐲स룃㐲ह맃㔲र뫃㔲ऱ믃㔲ल볃㔲ळ뷃㔲ऴ뻃
㔲व뿃 

Any help is appreciated. 任何帮助表示赞赏。

printf %c can't be used to generate UTF8 output. printf %c不能用于生成UTF8输出。 It only outputs single byte ASCII. 它只输出单字节ASCII。 UTF8 is single byte only for the first 128 characters that map to ASCII. UTF8仅为映射到ASCII的前128个字符的单字节。 After that, UTF8 is multibyte per character. 之后,UTF8每个字符都是多字节的。

See this answer for a method of generating non ASCII characters using wide characters. 有关使用宽字符生成非ASCII字符的方法,请参阅此答案。

How to iterate through unicode characters and print them on the screen with printf in C? 如何迭代unicode字符并使用printf在C屏幕上打印它们?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM