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 large ASCII file with QFile
Forum Updated to NodeBB v4.3 + New Features

Reading large ASCII file with QFile

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 5 Posters 2.1k Views 3 Watching
  • 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.
  • H Offline
    H Offline
    HB76
    wrote on last edited by
    #1

    Hi everyone !

    I'm trying to read data from a very large ASCII file (over 2 Gb) with QFile by reading the file line after line and processing each line before going to the next-one. To avoid application freeze during loading I used a Thread. My code works fine with smaller file (<500 Mb) but when I try to read large files, the program crash. Here is my code :

    QFile file(file_name);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return;
    
        try
        {
            int size = int(file.size());
            bool end = false;
    
            while(!end)
            {
                QString line = file.readLine();
    
                //...
                // Processing line
                //...
    
                read += line.toUtf8().size();
                emit loadingValueChanged(int(100*double(read)/double(size)));
            }
        } catch (...) {
                emit error(tr("Error - data loading failed"));
        }
    

    Should I use a memory mapped file or something like ?

    My project is configured with Qt 5.9.8 MinGW 32 bits.

    Thanks !

    kshegunovK 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @HB76 said in Reading large ASCII file with QFile:

      int size = int(file.size());

      This will not work when your file is larger than 2GB ...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        Its something else it crashes from than the file being big since you read it line by line.
        Your code
        while(!end) {

        looks a bit odd.

        Normally its done like.

        QFile inputFile(fileName);
        if (inputFile.open(QIODevice::ReadOnly))
        {
           QTextStream in(&inputFile);
           while (!in.atEnd())
           {
              QString line = in.readLine();
              ...
           }
           inputFile.close();
        }
        

        I read a 100 GB file like this so we have to find out why you crash.

        First. You have 100% sure its line in the files ?
        else it will read it all in one go :)

        H 1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          Its something else it crashes from than the file being big since you read it line by line.
          Your code
          while(!end) {

          looks a bit odd.

          Normally its done like.

          QFile inputFile(fileName);
          if (inputFile.open(QIODevice::ReadOnly))
          {
             QTextStream in(&inputFile);
             while (!in.atEnd())
             {
                QString line = in.readLine();
                ...
             }
             inputFile.close();
          }
          

          I read a 100 GB file like this so we have to find out why you crash.

          First. You have 100% sure its line in the files ?
          else it will read it all in one go :)

          H Offline
          H Offline
          HB76
          wrote on last edited by HB76
          #4

          @mrjj

          I tried your code but unfortunately the problem remains, and I it looks like the file reading is slower than using directly QFile methods.
          The strange thing is that the file loads correctly until the end (I connected a progressbar to the signal of my thread to visualize the loading progression), but when the loading is done, the thread send the error signal...

          The file is ASCII with line for sure.

          mrjjM 1 Reply Last reply
          0
          • H HB76

            @mrjj

            I tried your code but unfortunately the problem remains, and I it looks like the file reading is slower than using directly QFile methods.
            The strange thing is that the file loads correctly until the end (I connected a progressbar to the signal of my thread to visualize the loading progression), but when the loading is done, the thread send the error signal...

            The file is ASCII with line for sure.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @HB76
            Hi

            So the file reader is running in thread?

            Try to disable the emit loadingValueChanged for a test and see if it still crashes.

            You might be spamming the event loop which can crash.

            else i see no reason to crash even if file was 1 TB as we just read it line by line. (and not store each line)

            H 1 Reply Last reply
            0
            • mrjjM mrjj

              @HB76
              Hi

              So the file reader is running in thread?

              Try to disable the emit loadingValueChanged for a test and see if it still crashes.

              You might be spamming the event loop which can crash.

              else i see no reason to crash even if file was 1 TB as we just read it line by line. (and not store each line)

              H Offline
              H Offline
              HB76
              wrote on last edited by
              #6

              @mrjj

              Even with th signal disabled the application continues to crash...
              Is it possible that I run out of RAM ? Because each line can contain a lot of data (it is vectorial data file)

              mrjjM 1 Reply Last reply
              0
              • H HB76

                @mrjj

                Even with th signal disabled the application continues to crash...
                Is it possible that I run out of RAM ? Because each line can contain a lot of data (it is vectorial data file)

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                @HB76 said in Reading large ASCII file with QFile:

                Is it possible that I run out of RAM ?

                How large is one line ?
                Also how do you split it to values ?

                Yes, must be something like that then.

                is it a 32 bit or 64 bit app ?
                also if you run with debugger on, what line makes it crash ?

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  HB76
                  wrote on last edited by
                  #8

                  The longest line can have more than 30'000 characters.
                  Each line contain a keyword at the beginning which is separated from data thanks to the "/" char.
                  After that, data are separated with coma.
                  So I use the split method to get a QStringList of data that I convert in values by using ToInt() method of QString class.

                  My compiler version in 32 bits. Do you think it can have an impact of the reading ?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    Since you have a 32bit application you can't use more than 4GB of RAM. Is that the case with your application ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • H HB76

                      Hi everyone !

                      I'm trying to read data from a very large ASCII file (over 2 Gb) with QFile by reading the file line after line and processing each line before going to the next-one. To avoid application freeze during loading I used a Thread. My code works fine with smaller file (<500 Mb) but when I try to read large files, the program crash. Here is my code :

                      QFile file(file_name);
                          if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                              return;
                      
                          try
                          {
                              int size = int(file.size());
                              bool end = false;
                      
                              while(!end)
                              {
                                  QString line = file.readLine();
                      
                                  //...
                                  // Processing line
                                  //...
                      
                                  read += line.toUtf8().size();
                                  emit loadingValueChanged(int(100*double(read)/double(size)));
                              }
                          } catch (...) {
                                  emit error(tr("Error - data loading failed"));
                          }
                      

                      Should I use a memory mapped file or something like ?

                      My project is configured with Qt 5.9.8 MinGW 32 bits.

                      Thanks !

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @HB76 said in Reading large ASCII file with QFile:

                      My code works fine with smaller file (<500 Mb) but when I try to read large files, the program crash.

                      Please provide a backtrace of the crash.

                      Read and abide by the Qt Code of Conduct

                      H 1 Reply Last reply
                      1
                      • kshegunovK kshegunov

                        @HB76 said in Reading large ASCII file with QFile:

                        My code works fine with smaller file (<500 Mb) but when I try to read large files, the program crash.

                        Please provide a backtrace of the crash.

                        H Offline
                        H Offline
                        HB76
                        wrote on last edited by
                        #11

                        @kshegunov

                        The exception returned is std::bad_alloc.
                        I should try with 64 bits version, but I need to add the 64 bits MinGW compiler since I don't have it configured in Qt.

                        mrjjM kshegunovK 2 Replies Last reply
                        0
                        • H HB76

                          @kshegunov

                          The exception returned is std::bad_alloc.
                          I should try with 64 bits version, but I need to add the 64 bits MinGW compiler since I don't have it configured in Qt.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @HB76
                          Just install the Qt 64 bit MinGW Qt binary from the maintenance tool and you are good to go.

                          Also, have a look at
                          https://doc.qt.io/qt-5/qstringref.html
                          which might provide a huge boost since it will not copy the strings and all you want is to call toInt on it.

                          1 Reply Last reply
                          0
                          • H HB76

                            @kshegunov

                            The exception returned is std::bad_alloc.
                            I should try with 64 bits version, but I need to add the 64 bits MinGW compiler since I don't have it configured in Qt.

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by
                            #13

                            @HB76 said in Reading large ASCII file with QFile:

                            The exception returned is std::bad_alloc.

                            This doesn't tell me where the memory was not enough. But you have your answer anyway, you don't have enough memory.

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            2
                            • H Offline
                              H Offline
                              HB76
                              wrote on last edited by
                              #14

                              Ok guys, I configured the project with an older MinGW version in 64 bits and it works ! It was just a simple problem... I will try to install the most recent MinGW version and it will be perfect.

                              Thanks all for your help !

                              1 Reply Last reply
                              1

                              • Login

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