0

I am trying to read the contents of a text file into Glib::ustrings. 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.

v010dya
  • 5,296
  • 7
  • 28
  • 48
  • OT: `while (fin)` is the same as `while (fin.good())` which is just as bad as [`while (!fin.eof())`](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – Some programmer dude Dec 18 '22 at 10:55
  • 2
    [The class `Glib::ConvertError`](https://developer-old.gnome.org/glibmm/stable/classGlib_1_1ConvertError.html) has a public member function `code()` that returns a value of the enum type `Glib::ConvertError::Code`, which may help to identify the cause of the error. – kotatsuyaki Dec 18 '22 at 10:58

1 Answers1

0

For me the answer actually was not in the reading, but in the output. After changing it to

std::cout << str.raw() << std::endl;

Everything worked like a charm.

v010dya
  • 5,296
  • 7
  • 28
  • 48