#include <fstream>
#include <iostream>
#include <string>
#include <locale>
static std::wstring readStream(std::wistream& s)
{
std::locale def_lc("en_GB.utf8");
s.imbue(def_lc);
std::wstring temp = L"";
std::wstring retval = L"";
while(getline(s, temp))
{
retval += temp;
}
return retval;
}
And this is the main method. It simply calls 'readstream' with a (wide) file input stream. The file in question contains 1 paragraph of German text. This paragraph happens to contain 'special' characters.
int main()
{
std::wifstream fin("/home/joris/input.txt");
std::locale def_lc("en_GB.utf8");
std::wcout.imbue(def_lc);
std::wcout << readStream(fin) << std::endl;
// return
return 0;
}
Lastly, this is the input file:
Die Fastnacht des Jahres 1588 sollte in Salzburgs Trinkstube mit einem
glänzenden Fest, Schmaus und Tanz der Bürgergeschlechter gefeiert
werden, dem beizuwohnen der junge Landesherr, Erzbischof Wolf Dietrich,
in Gnaden der Bürgerdeputation versprochen hatte.
Executing the program does not really yield the expected result. The text is garbled. It doesn't handle the special characters.
I know I'm using utf8 here. But using 'en_GB.utf16' gives me an error:
terminate called after throwing an instance of 'std::runtime_error'
what(): locale::facet::_S_create_c_locale name not valid
Aborted
Ideally, I would like a system-independent solution. Without external libraries.
Aucun commentaire:
Enregistrer un commentaire