Reading in from a streaming text file

  • 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
    :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.

  • 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

  • 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?

  • 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()

  • 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     
       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.
         if ( |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
                     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
    return a.exec&#40;&#41;;


  • Hi,

    what is the question?

    Suggestion: Open New Thread for new Question

  • 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.

  • 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\

  • 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.

Log in to reply