Writing data into a text-file



  • Dear all,

    my qt-program calculates data and I want to store them in a text-file.
    But the problem is, that the program overrides the same line every time.

    QFile file("C:/Users/frti1017/Documents/GUI-Vision/result_storage.txt");
          if(file.open(QIODevice::WriteOnly | QIODevice::Text))
          {
              // We're going to streaming text to the file
              QTextStream stream(&file);
    
              stream << "Center Point: " << iter_result[0] << "  " << iter_result[1]
                        << "  " << iter_result[2] << " Rotation: " << iter_result[3] <<'\n';
    
              file.close();
              qDebug() << "Writing finished";
          }
    

    How can I write every line of data in a new line?

    Thanks a lot.

    Kindly regards,
    Timo



  • I have difficulties to see where your data are computed. The showed snippet looks valid, but it is meant to open a file, write a line and close the file There is nothing to write more than one line in your code.


  • Moderators

    @Timo_F When opening the file add QIODevice::Append:

    if(file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
    

    Else you're overwriting the file each time you open it.



  • @Timo_F If you call this function more than once, you will overwrite the file and the previous line evry time.

    If you just open the file and write into it you start writing at the beginning of the file.

    The easiest wa, in this case, y is to open the file with QIODevice::Append
    link



  • It works, thank you all a lot :-) :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.