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 in from a streaming text file
Forum Updated to NodeBB v4.3 + New Features

Reading in from a streaming text file

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 5.2k 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.
  • J Offline
    J Offline
    jde08
    wrote on last edited by
    #1

    Hi,

    I'm trying to read in a line from an instrument that streams in data into a txt file in the format:

    :SA, -1.05, -0.83,190.98
    :TS,13050814070409,35.0,+22.4, 0.0,1528.0, 0
    :BI,-32768,-32768,-32768,-32768,V
    :BS,-32768,-32768,-32768,V
    :BE,-32768,-32768,-32768,V
    :BD, +0.00, +0.00, +0.00, 0.00, 0.00

    I only need the three data points after the :SA and the first data point after :TS.

    I wrote this in c++ using getline, but implementing it into Qt has proven above me.
    I am brand new to Qt, have only been using it for 2 days and would really appreciate any pointers I could get.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      you can use QTextStream::readLine() instead of getline().
      Once you read a line you can choose one of

      • Use QTextStream to read items
      • QString::split() to split line in tokens

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jde08
        wrote on last edited by
        #3

        After the line is split with QString::split() how do I compare the values of the first list entry to check if its a line I could use.

        Also after I split up the line how would I assign the values to labels?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Hi,

          QStringList is inherited from QList, so you can use operator [] to access to elements.

          @
          QStringList tokens = line.split (",");
          if (tokens[0] == ":SA") {
          // Do something
          }
          else if (tokens[0] == ":TS") {
          // Do something
          }
          @

          To assign text to labels you can use QLabel::setText()

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jde08
            wrote on last edited by
            #5

            Thanks for your help so far!

            I have another question say the .txt file I want to read is located in C:\Program Files...

            How would I route the program to that directory?

            Here is the code I have written so far.
            @ #include "mainwindow.h"
            #include <QApplication>
            #include <QGridLayout>
            #include <QLabel>
            #include <QtGui>
            #include <iostream>
            #include <stdio.h>
            #include <QString>
            #include <QStringList>
            #include <QFile>
            #include <QTextStream>
            using namespace std;

            int main(int argc, char *argv[])
            {
            QApplication a(argc, argv);

            QWidget w;
            
            int roll;
            int pitch;
            int heading;
            int i=1;                //counter for next entry
            int filenum = 0;
            int filenum1= filenum+1;
            int n=0;                //counter for current entry
            
            QString qstr1 = QString::number(roll);
            QString qstr2 = QString::number(pitch);
            QString qstr3 = QString::number(heading);
            QString filename = QString("ADCP%1_000000").arg(filenum);
            QString filename1 = QString("ADCP%1_000000").arg(filenum1);
            
            
            QFile file&#40;filename1&#41;;
            if (file.exists())              //Looks for newest ADCP file
            {
                filenum++;                 //if ADCP+1 exists starts from that file     
            }
            else
            {
               QFile file&#40;filename&#41;;
                if(i!=n)                //if counters don't match starts current counter at zero
                {                       //to read from start of the file and opens file.
                    n=0;
                    file.open(QIODevice::ReadOnly |QIODevice::Text);
                }
                 if ( file.open(QIODevice::ReadOnly |QIODevice::Text) )
                 {
                     QTextStream in(&file);
                     QString line = in.readline();            //reads and splits lines
                     QStringList tokens = line.split (",");
                     if (tokens[0] == ":SA")
                     {
                         n++;                       //increases current counter whenever :SA appears.
                         if (n==i)                  //when current counter = next counter displays data
                         {
                             qstr1= tokens[1];
                             qstr2= tokens[2];
                             qstr3= tokens[3];
                         }
            
                     }
                 else if (tokens[0] == ":TS")
                     {
                         if (n==i)                  //Same as above and increases next counter for the next reading
                         {
                             i++;
                             QString timestring = tokens[1];
                         }
                     }
                   }
              }
            
            QGridLayout* mainLayout = new QGridLayout(&w);
            QLabel* rolllabel = new QLabel("Roll:   ");
            QLabel* pitchlabel = new QLabel("Pitch:  ");
            QLabel* headinglabel = new QLabel("Heading:  ");
            QLabel* rollreading = new QLabel(qstr1);
            QLabel* pitchreading = new QLabel(qstr2);
            QLabel* headingreading = new QLabel(qstr3);
            
            mainLayout->addWidget(rolllabel,0,0);               //Prints to gui
            mainLayout->addWidget(rollreading,0,1);
            mainLayout->addWidget(pitchlabel,1,0);
            mainLayout->addWidget(pitchreading,1,1);
            mainLayout->addWidget(headinglabel,2,0);
            mainLayout->addWidget(headingreading,2,1);
            
            
            w.show();
            
            return a.exec&#40;&#41;;
            

            }
            @

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by
              #6

              Hi,

              what is the question?

              Suggestion: Open New Thread for new Question

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tobias.hunger
                wrote on last edited by
                #7

                Provided your txt file is not static then the file should not be in C:\Program Files at all. The stuff there should not change after installation. You will also need elevated privileges just to write there.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jde08
                  wrote on last edited by
                  #8

                  The text file is written in the data folder of an instrument interface. That is why I'm trying to read from (not write) a C:\Program Files directory.

                  The question was how do I open a file in a certain directory. For me this is C:\Program Files\RD Instruments\VmDas\data\

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tobias.hunger
                    wrote on last edited by
                    #9

                    Have you tried this?

                    @QString filename = QString("C:/Program Files/RD/Instruments/VmDas/data/ADCP%1_000000").arg(filenum);@

                    I still think that this is the wrong place for files like that;) %APPDATA% is where Microsoft intends application data to be stored.

                    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