Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Can't print utf-8 string in the console

I am using TinyXML to parse .xml file in UTF-8 encoding.

bool load_kb(const std::string &filename, kb_t<std::string> **kb) {
    TiXmlDocument doc(filename.c_str());
    bool is_loaded = doc.LoadFile(TIXML_ENCODING_UTF8);
    if (!is_loaded) return false;
    auto root = doc.RootElement();
    *kb = new kb_t<std::string>(root->Attribute("name"));

    return true;
}

My kb_t class:

template <typename val_t>
class kb_t {
    const std::string &m_name;
public:
    kb_t(const std::string &name) : m_name{name} {}
    const std::string &name() { return m_name; }
};

And main function where I am trying to print a knowledge db name:

int main() {
    std::setlocale(LC_ALL, "en_US.UTF-8");
    kb_t<std::string> *kb;

    if (!xpertium::load_kb("kb.xml", &kb)) {
        std::cout << "Can't load knowledge database.\n";
    }

    std::cout << "KB name: " << kb->name() << std::endl;

    delete kb;

    return 0;
}

But I see in the console this only:

KB name: 0s�A���A� ��A����A�P��A�0i�A��e�A��[�A����A�PX�A��V�A���A�i�A��a�A�Y�A�@X�A��a�A��A����A���A�`��A���A����A����A� ��A��A�0��A����A�`��A�P��A���A����A����A����A�P��A����A��A����A���A� ��A���A����A����A� ��A��A�0��A����A�`��A�P��A���A����A����A����A�P��A����A��A����A��4B����A����A�`�A� ��A���A�0�A� �A��A��~�A�0�A��s�A��~�A�n    �A�����V���A����A����A����A���A�J��A�`��A��'�A����A����A�HT�A�V�A�t�A����A���A�R��A�p��A�

Although in the debugger I can see that name="materials" as it must be.

P.S. I am using Ubuntu 18.04 if it's important.

Comments