Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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;
    
        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



    • 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


  • Lifetime Qt Champion

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

    James



  • @mrjj Its a text file.


  • Lifetime Qt Champion

    @James-Sprinks
    yes, that is very clear.
    i mean how is each line saved ?
    xx,yyy,444
    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?

    J



  • You can find out using

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


  • This post is deleted!

Log in to reply