UTF-8 Code error



  • Hi guys

    I'm building an app for spanish language and I have some trouble reading a txt file one i have some characters like á,é,í,ó,ú, Á, É,Í,Ó,Ú, Ñ....

    my code is this one:

    @
    QFile file("/home/freddy/probar");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream in(&file);
    in.setCodec("UTF-8"); // change the file codec to UTF-8.

    while(!in.atEnd())
    {
    QString line = in.readLine();
    qDebug() << line.toLocal8Bit(); // convert to locale multi-byte string
    }
    exit(0);
    @

    with this example, when in the pace of any some character exposed above, i get this one: �

    after that, I create some new file with this content: áááááéééÁÁÁÁÁÁÑÑÑÑññññññ

    and the code work fine.. I'm thinking that the first file is not coded in UTF-8...

    so, what can I do to get the correct characters from my first file ???

    I'm using Archlinux, Qt5.2.1, my OS language is English and my locale command give me this output:

    @LANG=en_US.UTF-8
    LC_CTYPE="en_US.UTF-8"
    LC_NUMERIC="en_US.UTF-8"
    LC_TIME="en_US.UTF-8"
    LC_COLLATE="en_US.UTF-8"
    LC_MONETARY="en_US.UTF-8"
    LC_MESSAGES="en_US.UTF-8"
    LC_PAPER="en_US.UTF-8"
    LC_NAME="en_US.UTF-8"
    LC_ADDRESS="en_US.UTF-8"
    LC_TELEPHONE="en_US.UTF-8"
    LC_MEASUREMENT="en_US.UTF-8"
    LC_IDENTIFICATION="en_US.UTF-8"
    LC_ALL=@

    best regards

    freddy



  • Since you think your first file is not UTF-8 encoded, then check that in order to be sure and then figure out which is the used encoding.
    Or since your locale settings are using UTF-8, just convert your first file into UTF-8, for instance using the "C-x C-m f" in emacs. If this file was obtained by copy-paste from a web page or from a pdf, it might contain special characters which prevent the UTF-8 encoding to be used.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.