Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Opening a txt file
Qt 6.12 LTS is out! See what's new in the release blog

Opening a txt file

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 2.6k Views 1 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.
  • James SprinksJ Offline
    James SprinksJ Offline
    James Sprinks
    wrote on last edited by
    #1

    Hi All.

    I am attempting to open a txt file in order to scrape data from it, line by line;

    void logscrape::scrape()
    {
        int size;
    
        QFile sedation("C:/PMPS/build-PMPSv1-Desktop_Qt_5_9_2_MSVC2017_64bit-Debug/SedationData/" + sedName);
        QFile log("C:/PMPS/build-PMPSv1-Desktop_Qt_5_9_2_MSVC2017_64bit-Debug/LogData/" + logName);
        QStringList csvtime;
        QStringList logtime;
        QStringList tcitarget;
        QStringList tciplasma;
        QStringList tcieffect;
    
        log.open(QIODevice::ReadOnly);
    
        while (!log.atEnd())
        {
            QByteArray logline = log.readLine();
            if (logline.contains("TCITAR"))
            {
                int timeindex = logline.indexOf("UTC");
                int targetindex = logline.indexOf("TCITAR");
                int plasmaindex = logline.indexOf("TCIPLA");
                int effectindex = logline.indexOf("TCIEEFF");
    
                logtime.append(logline.mid(timeindex - 9, 8));
                tcitarget.append(logline.mid(targetindex + 7, 4));
                tciplasma.append(logline.mid(plasmaindex + 7, 4));
                tcieffect.append(logline.mid(effectindex + 8, 4));
    
                size = logtime.size();
    
                qDebug() << logtime << tcieffect;
                qDebug() << size;
            }
    
        }
    
    

    Everything works fine in terms of scraping the data - however the program just hangs after the line qDebug() << size. Any ideas?

    James

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2
      • log.open(QIODevice::ReadOnly); should be opened in text mode log.open(QIODevice::ReadOnly | QIODevice::Text);
      • Use QTextStream when dealing with text, your method fails miserably with any non-ASCII encoded character. You can find an example in the docs
      • You should probably use a QRegularExpression to extract your data instead of indexOf+mid

      hangs after the line qDebug() << size

      The problem is not there

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

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

        Hi
        I wondering how the input lines looks like?
        Its not a CSV file but custom format ?

        James SprinksJ 1 Reply Last reply
        0
        • VRoninV VRonin
          • log.open(QIODevice::ReadOnly); should be opened in text mode log.open(QIODevice::ReadOnly | QIODevice::Text);
          • Use QTextStream when dealing with text, your method fails miserably with any non-ASCII encoded character. You can find an example in the docs
          • You should probably use a QRegularExpression to extract your data instead of indexOf+mid

          hangs after the line qDebug() << size

          The problem is not there

          James SprinksJ Offline
          James SprinksJ Offline
          James Sprinks
          wrote on last edited by
          #4

          @VRonin Hi

          Thanks for the advice - however as I said it is scraping the data fine, this is not the cause of the hang.

          James

          1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            I wondering how the input lines looks like?
            Its not a CSV file but custom format ?

            James SprinksJ Offline
            James SprinksJ Offline
            James Sprinks
            wrote on last edited by
            #5

            @mrjj Its a text file.

            mrjjM 1 Reply Last reply
            0
            • James SprinksJ James Sprinks

              @mrjj Its a text file.

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

              @James-Sprinks
              yes, that is very clear.
              i mean how is each line saved ?
              xx,yyy,444
              or how does it look?

              1 Reply Last reply
              2
              • James SprinksJ Offline
                James SprinksJ Offline
                James Sprinks
                wrote on last edited by
                #7

                Hi All

                I solved the issue. I now have another one, I am trying to open a csv in readwrite mode, and get this error:

                QIODevice::read (QFile, "C:\PMPS\build-PMPSv1-Desktop_Qt_5_9_2_MSVC2017_64bit-Debug\SedationData\logtest_sed.csv"): device not open

                The path is correct... so why won't it open?

                J

                JonBJ 1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  You can find out using

                  if(!csvFile.open(QIODevice::ReadOnly | QIODevice::Text))
                  qDebug() << csvFile.errorString();
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  4
                  • James SprinksJ James Sprinks

                    Hi All

                    I solved the issue. I now have another one, I am trying to open a csv in readwrite mode, and get this error:

                    QIODevice::read (QFile, "C:\PMPS\build-PMPSv1-Desktop_Qt_5_9_2_MSVC2017_64bit-Debug\SedationData\logtest_sed.csv"): device not open

                    The path is correct... so why won't it open?

                    J

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9
                    This post is deleted!
                    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
                    • Unsolved