[Solved] Read special characters from a csv-file



  • Hi

    I've got a problem with reading content from a file. And since I can't find any decent solution on the interwebs, I though I asked it here.

    My file contains special characters like Š. But when I do a readLine() on my file it returns something strange.

    In the file: 'Roman';'Šebrle'
    In my QString line: 'Roman';'Šebrle'
    And when I want to insert it in my database: UPDATE athletes SET lastname = '?ebrle', middlename = '', firstname = 'Roman'...

    So can anyone help me with this? How to get that Š?

    Thanks in advance!



  • When using the QFile.readLine you will store data in a char buffer. This is a 8 bits variable only capable of holding ASCII values. The special characters are available when using the QTextStream to read/write data to the file.
    With the QTextCodec class you are able to transform into all kind of character sets. Even automatic detection of the best fitted text codec is possible.
    So it could be more like this:
    @
    if (file.open())
    {
    QTextStream in(file);
    QStringList inputStrings;

    in.setAutoDetectUnicode (true);
    while (!in.atEnd())
    {
    in >> inputStrings.append();
    }
    }
    @
    Don't have my Qt with me, so no idea if the in>>inputStrings.append() will work!
    Good luck with the coding!



  • But can it read line by line? Cause that has to stay.

    I just tried it. But i don't think it's a good solution. Cause I have to look for spaces myself.

    It solves my special character problem though.



  • Read the Qt documentation:
    http://qt-project.org/doc/qt-4.8/qtextstream.html#readLine

    What do you mean with "look for spaces myself"?
    @QStringList spaceSeparatedStrings = singleString.split(" ");@
    ?



  • It seperates on spaces.

    But thanks. That I didn't think of that myself!
    Quiet stupid :)
    Thanks for the reply guys. I think it works just fine now.


Log in to reply
 

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