Important: Please read the Qt Code of Conduct -

Opening a txt file

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


    •; should be opened in text mode | 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

  • Lifetime Qt Champion

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

  • @VRonin Hi

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


  • @mrjj Its a text file.

  • Lifetime Qt Champion

    yes, that is very clear.
    i mean how is each line saved ?
    or how does it look?

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


  • You can find out using

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

  • This post is deleted!

Log in to reply