Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] The correct way to read a file line by line
QtWS25 Last Chance

[Solved] The correct way to read a file line by line

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 26.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alexandros
    wrote on last edited by
    #1

    I've read an infinite count of posts and the documentation, but still, I've not managed to make it work. This is my code:
    @
    int main(){
    QFile file("/home/alex/1.txt");
    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;
    //OR qDebug() << line.toLocal8Bit; None of these ways work
    }
    return 0;
    }
    @
    I'm not very sure about in.setCodec("UTF-8"); but I know that UTF-8 accepts special characters that I want, like Greek, Chinese, Arabic, French with tones etc.

    I just want to read the contents of any text file without the concern of special characters...

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sfilippidis
      wrote on last edited by
      #2

      Well, I do not remember using QTextStream::setCodec for some Greek strings I had used! Instead, if I recall correctly, I had used QString::fromWCharArray for each one of the respective strings.

      https://www.filippidis.name/

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alexandros
        wrote on last edited by
        #3

        Hello Stavros, thanks for the answer; can you please post some lines of code just like I did in my original post so as to figure out what you mean?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sfilippidis
          wrote on last edited by
          #4

          [quote author="alexandros" date="1342202344"]Hello Stavros, thanks for the answer; can you please post some lines of code just like I did in my original post so as to figure out what you mean?[/quote]

          Well, I found the source code I had written, and I actually had used a QDataStream, not a QTextStream.

          However, in your example, now that I think of it, my previous suggestion will not help you, unless you are trying to WRITE to a file a string literal. Example (for assignement to a QString variable):

          @QString testText = QString::fromWCharArray(L"Δοκιμή");@

          So, to analyze your problem, what is the exact error you get with the code you initially posted? How did you create the .txt file that you use? Which Qt version on what platform (Operating System)?

          https://www.filippidis.name/

          1 Reply Last reply
          0
          • A Offline
            A Offline
            alexandros
            wrote on last edited by
            #5

            I'm in ubuntu 12.04 and the about dialog of my qtcreator says:
            Qt Creator 2.4.1
            Based on Qt 4.8.0 (32 bit)

            But, why does it matter how I've written to the file? I think that, no matter how I've written to the file, I should be able to read it all through, passing the special characters as well.
            I don't mind using QDataStream, after all.

            In this occassion, I've written the file using Gedit, but I'd like to be able to read from any text file.

            I'm not getting any errors, but when I do a
            @
            qDebug() << line;
            @
            all I get is the latin characters, some symbols and the numbers, it is seems that it omits all the special characters.
            E.g.:
            input_file:
            @
            PΠΠP
            ΣΣWWΣΣ
            @
            will print:
            @PP
            WW@

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sfilippidis
              wrote on last edited by
              #6

              Just a guess: Why don't you output to a QWidget (which is capable of handling UTF-8) like a QLabel? I am not sure if qDebug supports it.

              https://www.filippidis.name/

              1 Reply Last reply
              0
              • A Offline
                A Offline
                alexandros
                wrote on last edited by
                #7

                Hah, you are right -> http://i.imgur.com/ownx6.png <-

                The thing is, I am planning to store each line of the input_file inside a QStringList, there shouldn't be a problem reading from there. Should it?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sfilippidis
                  wrote on last edited by
                  #8

                  Nice! Since your question was successfully answered, please edit the title of this thread so that it begins with [SOLVED]

                  https://www.filippidis.name/

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    alexandros
                    wrote on last edited by
                    #9

                    [quote author="Stavros" date="1342209242"]Nice! Since your question was successfully answered, please edit the title of this thread so that it begins with [SOLVED][/quote]

                    That should be automatic ;)

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mlong
                      wrote on last edited by
                      #10

                      bq. That should be automatic ;)

                      It should, but it's not. I think there's a feature request floating around for it in the bugtracker.

                      bq. The thing is, I am planning to store each line of the input_file inside a QStringList, there shouldn’t be a problem reading from there. Should it?

                      Shouldn't at all. QStrings are very unicode-friendly.

                      Software Engineer
                      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        alexandros
                        wrote on last edited by
                        #11

                        [quote author="mlong" date="1342211423"]Shouldn't at all. QStrings are very unicode-friendly.[/quote]
                        Nice. True, I've already tested it and you are correct!

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved