简体   繁体   English

Linux Ubuntu中的文件内输出unicode符号

[英]In-file output unicode symbols in Linux Ubuntu

I wrote a translator from decimal number to Unicode characters. 我写了一个从十进制数字到Unicode字符的转换器。

In the input file, we have several numbers that after the translation will give out some character. 在输入文件中,我们有几个数字,这些数字在翻译后会给出一些字符。 For example, the euro sign, represented as 226 130 172 at the outset, will look exactly like a euro sign. 例如,一开始用226 130 172表示的欧元符号看起来就像是欧元符号。 The problem is that I can not output this to a file, but I can output it to the console. 问题是我无法将其输出到文件,但可以将其输出到控制台。 In the program there is a method with extended fstream, it allows to output symbols up to 4 bytes to the console. 该程序中有一个扩展了fstream的方法,它允许将最多4个字节的符号输出到控制台。 But there is nothing in the output file, and I do not understand why.My friend uses some way to output, redirecting the normal cout stream to a file, but, as I understand, this method will only work on Windows. 但是输出文件中什么也没有,我也不明白为什么。我的朋友使用某种方式输出,将普通的cout流重定向到文件,但是据我所知,这种方法仅适用于Windows。 I use Ubuntu 16.04, this method does not work for me. 我使用Ubuntu 16.04,这种方法对我不起作用。 I tried to configure the gedit (standard text editor in Ubuntu) to display, but I did not succeed. 我试图配置gedit(Ubuntu中的标准文本编辑器)进行显示,但没有成功。 In this code, I first open the extended streams, and then the following code goes. 在此代码中,我首先打开扩展流,然后执行以下代码。

int input, result = 0;

if(!fin.is_open()){
    cout << "Не удалось открыть файл!" << endl;
}
else{
    while(fin >> input){
        if(input >= 240){
            input -= 240;
            result += input << 18;
            fin >> input;
            input -= 128;
            result += input << 12;
            fin >> input;
            input -= 128;
            result += input << 6;
            fin >> input;
            input -= 128;
            result += input;
            wcout << (wchar_t)result << endl;
            fout << (wchar_t)result;
        }
        else if(input >= 224){
            input -= 224;
            result += input << 12;
            fin >> input;
            input -= 128;
            result += input << 6;
            fin >> input;
            input -= 128;
            result += input;
            wcout << (wchar_t)result << endl;
            fout << (wchar_t)result;
        }
        else if(input >= 192){
            input -= 192;
            result += input << 6;
            fin >> input;
            input -= 128;
            result = input;
            wcout << (wchar_t)result << endl;
            fout << (wchar_t)result;
        }
        else{
            wcout << (wchar_t)input << endl;  
            fout << (wchar_t)input;
        }
    }
}

  fin.close();
  fout.close();
  return 0;
}

You must declare fout as std::wofstream not std::ofstream for output wchar into a file. 您必须将fout声明为std :: wofstream而不是std :: ofstream,才能将wchar输出到文件中。 Example: 例:

wchar_t *temp = L"wchar";
std::wofstream wofs;
wofs.open("output.txt", std::ios::out);
wofs << L"Test\n";
wofs << temp;
wofs.close();

You can refer this link : Outputting 'wchar_t*' to an 'ofstream' 您可以参考以下链接:将'wchar_t *'输出到'ofstream'

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

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