Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Writing data into a text-file

Writing data into a text-file

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 37.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Timo_F
    wrote on 23 May 2017, 07:35 last edited by
    #1

    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

    J J 2 Replies Last reply 23 May 2017, 07:40
    0
    • J Offline
      J Offline
      JohanSolo
      wrote on 23 May 2017, 07:38 last edited by JohanSolo
      #2

      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.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • T Timo_F
        23 May 2017, 07:35

        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

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 23 May 2017, 07:40 last edited by
        #3

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        4
        • T Timo_F
          23 May 2017, 07:35

          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

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 23 May 2017, 07:41 last edited by J.Hilk
          #4

          @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


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • T Offline
            T Offline
            Timo_F
            wrote on 23 May 2017, 07:46 last edited by
            #5

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

            1 Reply Last reply
            1
            • L Offline
              L Offline
              l099ys74r
              wrote on 22 Mar 2021, 13:38 last edited by
              #6

              thx alot pal, it is usefull

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved