Pagrindinis > Programavimas, Studijos KTU > C++. How to print non ASCII characters?

C++. How to print non ASCII characters?

Rugsėjis 9th, 2009 Ernestas Kardzys Komentuoti Komentarai

We have some additional letters in the Lithuanian language: ą, č, ę, ė, į, š, ų, ū, ž (if you can’t see them – change the encoding of your browser into Unicode). The point is, that cout can not print those letters. So, how to print Unicode letters in the console?

The solution:

ernestas@ernestas-laptop:~$ g++ code.cpp
ernestas@ernestas-laptop:~$ ./a.out
Žuvis
Šamas
Paštas
ernestas@ernestas-laptop:~$ cat code.cpp
#include <iostream>
#include <fstream>
#include <wchar.h>
using namespace std;

void print(const wchar_t *text) {
wcout << text << endl;
}

int main() {
locale::global(locale(“en_US.utf8″));
wcout << L”Žuvis” << endl;
print(L”Šamas”);
wchar_t text[] = L”Paštas”;
print(text);

return 0;
}
ernestas@ernestas-laptop:~$

P.S. The translation:

Žuvis – The Fish
Šamas – The Sheatfish
Paštas – The Post

  1. NForce
    Rugsėjis 16th, 2009 į 02:02 | #1

    Keistas dalykas, bet pas mane konsolėj be jokių papildomų include’ų ir wcout’ų rodomos lietuviškos raidės. Iškyla natūralus klausimas: kam tada visa tai su wchar ir t.t.?

  2. Rugsėjis 16th, 2009 į 09:40 | #2

    Linux’e?

    string text = “ąčęėęį”;

    cout << text << endl;

    Taip man irgi su gcc veikia :) Tik ant Visual Studio nenori ;/

  1. Citatų nėra.