0

I wrote a translator from decimal number to Unicode characters.

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. 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. 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. I use Ubuntu 16.04, this method does not work for me. I tried to configure the gedit (standard text editor in Ubuntu) to display, but I did not succeed. 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;
}
Biffen
  • 6,249
  • 6
  • 28
  • 36

1 Answers1

0

You must declare fout as std::wofstream not std::ofstream for output wchar into a file. 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'

Le Ngoc Thuong
  • 279
  • 2
  • 8
  • Can I use "L" to some %string_name% without printing string value? For example: wchar_t *temp = L (%string_name%); –  Mar 09 '18 at 12:04
  • 1
    If string_name is wchar_t you can assign it to another wchat_t without "L": Ex: `code` wchar_t *string_name = L"abc"; wchar_t *temp = string; But if string_name is not wchar_t (string or char) you cannot directly assign it to a wchar_t variable, you must convert it. – Le Ngoc Thuong Mar 10 '18 at 06:45
  • Thx very much ^_^ –  Mar 10 '18 at 07:26