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

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.


  • Lifetime Qt Champion

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


  • Moderators

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

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

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



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



  • thx alot pal, it is usefull


Log in to reply