Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Writing to the file continuously in Qt (realtime data)?



  • Hi,

    I am building a GUI application with Qt which takes sensor data in realtime from a sensor (via Bluetooth LE) and writes it to a file.

    I have a function, which transforms the raw data coming from the sensor and saves these data as key-value pairs in a map. This function is a slot and is called every time new data is available (~every second). So, after transforing these data, I want to call a function writeDatraToFile which takes this map and writes the values in a txt. file. I have this approac so far:

    void Data::transformDataSlot(QString newSensorValue){
    
    //make some calculations and save this into a map     QMap<QString, float> sensorData;
    
    wirteDataToFile(directory,fileName, sensorData);
    }
    
    void Data::wirteDataToFile(QString directory, QString fileName, QMap<QString, float> sensorData)
    {
    
    /* Try and open a file for output */
        
            QFile file(directory.replace('\\', '/') + "/" + fileName + ".txt");
            file.open(QIODevice::Append);
        
        
            /* Check it opened OK */
            if(!outputFile.isOpen()){
            qDebug() <<"- Error, unable to open" << outputFilename << "for output";
            return ;
            }
    
            /* Point a QTextStream object at the file */
            QTextStream outStream(&outputFile);
    
            /* Write the line out of the map the file */
            outStream <<"test \n";
    
            /* Close the file */
        
            file.close();
    }
    

    For this approach, I have read:

    Note that opening the file for every single line is an inefficient solution, especially if you have many, many lines to write. >Opening the file once, then writing all the words to it before closing it, would work faster.

    So at first, I would say that I will delete the file.close() statement out of this function. Then I will connect a Slot to my Button "save" in my GUI and call "file.close" in this slot. Would this work? But what if an exception occurs while writing to the file or e.g. the application crashes or the user accidently quits the application without clicking on save. How can I make sure, that my file will still close? Maybe this?

    try:
        // writes to file
    finally:
        file.close()
    

  • Lifetime Qt Champion

    It does not help if you don't call close() since it's closed when the object goes out of scope.
    Make it a member of your class and call flush() after every write to make sure it's written to disk (as good as the OS supports this).


  • Lifetime Qt Champion

    It does not help if you don't call close() since it's closed when the object goes out of scope.
    Make it a member of your class and call flush() after every write to make sure it's written to disk (as good as the OS supports this).



  • @Christian-Ehrlicher said in Writing to the file continuously in Qt (realtime data)?:

    It does not help if you don't call close() since it's closed when the object goes out of scope.
    Make it a member of your class and call flush() after every write to make sure it's written to disk (as good as the OS supports this).

    Thank you, I did not know that.
    So my code looks like this now:

    //in header file I have QFile file;
    
    //in my .cpp:
    void Data::wirteDataToFile(QString directory, QString fileName, QMap<QString, float> sensorData)
    {
    
    
            file.setFileName(directory.replace('\\', '/') + "/" + fileName + ".txt");
            file.open(QIODevice::Append);
        
            if(!outputFile.isOpen()){
            qDebug() <<"- Error, unable to open" << outputFilename << "for output";
            return ;
            }
    
            QTextStream outStream(&outputFile);
    
            /* Write the line out of the map the file */
            outStream <<"test \n";
    
    }
    
    

    Then, I can call close, if my Button "save" is clicked.
    One further question:

    Since it is a continuous writing, that means that the file has to be opened jus once and the filename has to be set just once too. So I thought to have a flag called "firstTime" and every time check if firstTime is true (means that the file is not set or opened) then set the name and open otherwise do not. Are my considerations correct?:

    if (firstTime){
            file.setFileName(directory.replace('\\', '/') + "/" + fileName + ".txt");
            file.open(QIODevice::Append);
        }
    
    //after writing and so on 
    
    firstTime = false;
    

  • Lifetime Qt Champion

    @SpaceToon
    hi
    Yes you just want to open it once.
    But i dont see you calling flush anywhere ?



  • @mrjj said in Writing to the file continuously in Qt (realtime data)?:

    @SpaceToon
    hi
    Yes you just want to open it once.
    But i dont see you calling flush anywhere ?

    Oh, I forgot to paste the flush here, it is actually in my code directly after the write-statement. Thank you very much :)


Log in to reply