Glib::ustring, error while reading an utf-8 encoded file



Hi everybody,

I don't have experience about charset issues so I tried the following to
experiment:
(it's in C++ using glibmm and giomm but I guess the problem is not
related to that, it's just I don't understand something.)

*************************************
#include <giomm.h>
#include <iostream>
#include <glibmm.h>
#include <string>
int main(int argc, char** argv)
{
    Glib::init();
    Gio::init();
    if(argc!=2) return 1;
//open a file to read
    Glib::RefPtr<Gio::File> file=Gio::File::create_for_path(argv[1]);
    Glib::RefPtr<Gio::DataInputStream>
fin=Gio::DataInputStream::create(file->read());
    std::string line;
//try to read.
    try{
        int i=1;
        while(fin->read_line(line)){
            std::cout<<i<<": as-is: \""<<line<<"\""<<std::endl;
            std::cout<<i<<": through Glib::ustring:
\""<<Glib::ustring(line)<<"\""<<std::endl;
            ++i;
        }
    } catch(Glib::ConvertError error){
        std::cout<<std::endl<<"Glib::ConvertError:
"<<error.what()<<std::endl;
    }
    return 0;
}
*******************************************************************

Feading this utf-8 encoded file to the above program:

*******************************************************************
First line: plain text.
deuxième ligne: des accents ici et là...
*******************************************************************

results in:

1: as-is: "First line: plain text."
1: through Glib::ustring: "First line: plain text."
2: as-is: "deuxième ligne: des accents ici et là..."
2: through Glib::ustring: "
Glib::ConvertError: Invalid byte sequence in conversion input

my locale is using utf-8 (fr_CH).

The question is: How to import the file content in an Glib::ustring when
already knowing its encoding (not necessarily utf-8 though)?

I'm writing an application which will likely have to deal with asian
characters so I need to find out!

Thanks a lot!

Bastien



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]