I am trying to read the contents of a text file into Glib::ustring
s. As i understand for the gtkmm-list at gnome.org I should read a line into an std::string
and only then do the conversion. It works when the text is in ASCII, but fails when given something else.
Example code is:
#include <glibmm/ustring.h>
#include <fstream>
#include <iostream>
int main()
{
std::ifstream fin("testfile");
while(fin)
{
Glib::ustring str;
{
std::string s;
std::getline(fin, s);
std::cout << s << std::endl;
str.assign(s);
}
std::cout << str << std::endl;
}
return 0;
}
And the file contents (saved as UTF8) are
hello
привет
The first line is printed twice, meaning that ustring
is getting constructed just fine, but the second one comes out fine as std::string
, but then Glib::ConvertError
gets thrown. The code of the error is ILLEGAL_SEQUENCE
.
I have double checked and the file is as follows:
00000000: 68 65 6c 6c 6f 0a d0 bf d1 80 d0 b8 d0 b2 d0 b5 hello...........
00000010: d1 82 0a -- -- -- -- -- -- -- -- -- -- -- -- -- ...-------------
It does appear to be UTF-8. For example d1 82 are 11010001 10000010, which would make it the character 10001000010 in binary or U+0442, in other words Cyrillic 'т'. I have displayed every character or a read std::string
and have confirmed that the file was read correctly.