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. Reading file with QTextStream looping over file
QtWS25 Last Chance

Reading file with QTextStream looping over file

Scheduled Pinned Locked Moved General and Desktop
17 Posts 4 Posters 8.7k 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.
  • W Offline
    W Offline
    Woody
    wrote on last edited by
    #1

    Situation:
    In my program, I want to read a file. So I put my file in a QTextStream.
    @if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    QTextStream in(&file);
    in.setAutoDetectUnicode(true);

        while(!in.atEnd())
        {
            progressDialog.setValue(in.pos());
            QString line = QString();
            line = QString(in.readLine());
            if (!line.isEmpty())
            {
                 ...
            }
        }
    

    }@

    Problem:
    The file loops over the first 154 lines (of 235). And it doesn't stop.
    So it reads 65% of my file, loops over that 65% and just skips the other 35%.

    Anyone got an idea about this problem?

    File not found. Nobody leave the room!

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

      Is there anything special at line 154? Does it happen with any file?

      "Never memorize what you can look up in books."
      Albert Einstein

      1 Reply Last reply
      0
      • W Offline
        W Offline
        Woody
        wrote on last edited by
        #3

        I tried it with 2 files, one where he got to 87% and one to 65%

        There is nothing special on that line, I even tried to delete line 153, 154 and 155, but nothing else happened.

        I can't post the file, cause it contains confedential information. A screenshot with dummytext is possible:
        !http://wouterverbruggen.be/dummy.jpg(Dummy.jpg)!

        Even if I copy those 3 line to the first line, it does the same.

        File not found. Nobody leave the room!

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DKvita
          wrote on last edited by
          #4

          Try to remove QIODevice::Text option.

          1 Reply Last reply
          0
          • W Offline
            W Offline
            Woody
            wrote on last edited by
            #5

            Just tried it and isn't really working.

            If I don't put it in a QTextStream it works, but I need special characters to be read. So I need
            @in.setAutoDetectUnicode(true);@

            File not found. Nobody leave the room!

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

              If you expect to have unicode characters why don't you set codec to one of the utf's? Try "setting codec":http://qt-project.org/doc/qt-4.8/qtextstream.html#setCodec for your QTextStream object to UTF-8. Unless you set one globally before...

              "Never memorize what you can look up in books."
              Albert Einstein

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DKvita
                wrote on last edited by
                #7

                Can you send me your file 'as is'.

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  Woody
                  wrote on last edited by
                  #8

                  That doesn't help with the fact I don't have to use a QTextStream. I think (read: I'm quiet sure) that the problem has to do with the QTextStream. But where that is, I really haven't got a clue.

                  Edit: I can't really send you the file, sorry. But there isn't really something wrong with the line, nor the file itself.

                  File not found. Nobody leave the room!

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    DKvita
                    wrote on last edited by
                    #9

                    Ok, maybe, you may post which special characters appear in the file?

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      Woody
                      wrote on last edited by
                      #10

                      In this file the only special characters are ë (on Daniël, Joëlle, ...), é (on Conté, Romanée, ...), ö (on Buddenböhmer), ü (on Büller)

                      But none of these characters are on line 153, 154, 155, 156

                      File not found. Nobody leave the room!

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        DerManu
                        wrote on last edited by
                        #11

                        Does this:
                        @
                        while(true)
                        qDebug() << in.readLine();
                        @

                        read past that magic line 154?
                        Have you validated the strings that readLine returns? Maybe 154 is actually the last line in the file because some apparent multiple lines in the file are read joined together (for whatever crazy unicode reason).

                        bq. I tried it with 2 files, one where he got to 87% and one to 65%

                        I suppose the other file was also such a name list with that (maybe broken) unicode encoding. Do the following:
                        Open a text editor and type by hand some lines that reproduce the visual appearance (i.e. which characters come when) in one of the unicode files. This is to make sure there aren't any strange unicode characters inside. Then either compare them in a hex editor or just try to read that hand-crafted file (maybe multiply the lines by copy-pasting to 200 or so).
                        Now that should absolutely work, because your code is very simple. And if it does, you know that the original files contain crazy unicode control characters that need to be rectified first. It may be an effect due to the automatic unicode detection. If it's not detected correctly, who knows what happens. Why don't you set the codec yourself as proposed already?

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          DKvita
                          wrote on last edited by
                          #12

                          Do you sure the file is really unicode? It seems you have ASCII file that detected by QTextCodec as UTF-8.

                          1 Reply Last reply
                          0
                          • W Offline
                            W Offline
                            Woody
                            wrote on last edited by
                            #13

                            I don't think my decoding is the problem here.
                            If I don't try to decode it, it does exactly the same as with the autoDetect and the setCodec("utf-8").

                            I'll try to write some of it again, but that's not a solution for 30k lines-file :)

                            File not found. Nobody leave the room!

                            1 Reply Last reply
                            0
                            • W Offline
                              W Offline
                              Woody
                              wrote on last edited by
                              #14

                              Going to update Qt, I know have 4.7.4, my colleague has 4.8.2.
                              And on his PC it works fine.

                              File not found. Nobody leave the room!

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                sidewinder
                                wrote on last edited by
                                #15

                                It seems you may have been affected by bug "QTBUG-9814":https://bugreports.qt-project.org/browse/QTBUG-9814.

                                "Never memorize what you can look up in books."
                                Albert Einstein

                                1 Reply Last reply
                                0
                                • W Offline
                                  W Offline
                                  Woody
                                  wrote on last edited by
                                  #16

                                  Mmh that indeed could be possible. Thnx for the tip!

                                  File not found. Nobody leave the room!

                                  1 Reply Last reply
                                  0
                                  • W Offline
                                    W Offline
                                    Woody
                                    wrote on last edited by
                                    #17

                                    It is 'solved'. I installed Qt 4.8.2 and it does it's job just fine. Takes the whole file and treats every line properly.

                                    I don't think I need to put [SOLVED] in the title cause the problem in 4.7.4 isn't solved.

                                    File not found. Nobody leave the room!

                                    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