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. write to a different file each time of run
QtWS25 Last Chance

write to a different file each time of run

Scheduled Pinned Locked Moved Solved General and Desktop
qt5jsonwrite
7 Posts 4 Posters 1.0k 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.
  • D Offline
    D Offline
    deleted286
    wrote on 1 Mar 2021, 08:39 last edited by
    #1

    Is there any way to save this information in different txt files?

    I want to save this information, whenever i run the program. But in every run, they should save in different files

     void MainWindow::write_Json()
    {
          QFile file("/home/info.txt");
    
    
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        file.resize(0);
    
        QJsonArray root;
        QJsonObject joystick;
        QJsonObject info;
    
        info.insert("Date", QDateTime::currentDateTime().toString());
        info.insert("Author", "a");
        info.insert("Version", "1.0.0");
        info.insert("Port", currentPortName);
        root.append(info);
    
        joystick.insert("A",root);
        QJsonDocument doc(joystick);
    
        file.write(doc.toJson());
        file.close();
    
    }
    
    J 1 Reply Last reply 1 Mar 2021, 08:41
    0
    • D deleted286
      1 Mar 2021, 08:39

      Is there any way to save this information in different txt files?

      I want to save this information, whenever i run the program. But in every run, they should save in different files

       void MainWindow::write_Json()
      {
            QFile file("/home/info.txt");
      
      
          file.open(QIODevice::WriteOnly | QIODevice::Text);
          file.resize(0);
      
          QJsonArray root;
          QJsonObject joystick;
          QJsonObject info;
      
          info.insert("Date", QDateTime::currentDateTime().toString());
          info.insert("Author", "a");
          info.insert("Version", "1.0.0");
          info.insert("Port", currentPortName);
          root.append(info);
      
          joystick.insert("A",root);
          QJsonDocument doc(joystick);
      
          file.write(doc.toJson());
          file.close();
      
      }
      
      J Offline
      J Offline
      JonB
      wrote on 1 Mar 2021, 08:41 last edited by
      #2

      @suslucoder
      So specify a different filename when required. What else is there to say?

      D 1 Reply Last reply 1 Mar 2021, 08:43
      1
      • J JonB
        1 Mar 2021, 08:41

        @suslucoder
        So specify a different filename when required. What else is there to say?

        D Offline
        D Offline
        deleted286
        wrote on 1 Mar 2021, 08:43 last edited by
        #3

        @JonB so i will change the file name in everytime?
        I want automatize it. User cant be change the file name everytime

        S J J 3 Replies Last reply 1 Mar 2021, 08:48
        0
        • D deleted286
          1 Mar 2021, 08:43

          @JonB so i will change the file name in everytime?
          I want automatize it. User cant be change the file name everytime

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 1 Mar 2021, 08:48 last edited by
          #4

          Hi,

          @suslucoder said in write to a different file each time of run:

          @JonB so i will change the file name in everytime?
          I want automatize it. User cant be change the file name everytime

          Well, yes, it's your job to do so since it's your requirement. Therefore you have to generate an appropriate file name.

          Some small notes:

          • Your users will likely not appreciate that you flood their hard drives with files.
          • You are trying to write these files in a folder which they will not have access to
          • You do realise that if your user starts your application ten times in a row it will create ten files ?

          If you really want to log stuff, why not just properly implement that logging part ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • D deleted286
            1 Mar 2021, 08:43

            @JonB so i will change the file name in everytime?
            I want automatize it. User cant be change the file name everytime

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 1 Mar 2021, 08:51 last edited by
            #5

            @suslucoder surely you can think of at least one way to uniquify the file name automatically ?

            for example add the timestamp

            QFile file("/home/info." + QTime::currentTime().toString("mm.hh.ss.'txt'"));


            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.

            D 1 Reply Last reply 1 Mar 2021, 10:53
            2
            • D deleted286
              1 Mar 2021, 08:43

              @JonB so i will change the file name in everytime?
              I want automatize it. User cant be change the file name everytime

              J Offline
              J Offline
              JonB
              wrote on 1 Mar 2021, 08:52 last edited by JonB 3 Jan 2021, 08:53
              #6

              @suslucoder said in write to a different file each time of run:

              so i will change the file name in everytime?

              I don't know, is that what you want to do? Or, perhaps, you want to alternate between two names? Or something else? Sorry, I'm not a mind reader, so I don't know what you want to accomplish.

              User cant be change the file name everytime

              Your code shows nothing about the user choosing/changing any file name. It shows a name hard-coded.

              I want automatize it.

              OK, so do so. For example, perhaps you want to append a number to the file name, info1.txt, info2.txt, .... Then you might make your code look at what filenames already exists in an incrementing loop, and save as the next free one when you don't encounter an existing one. [EDIT Or, you might choose to do as @J-Hilk has shown, appending a datetime.]

              You really need to state what you are actually trying to achieve when asking a question, then you get appropriate answers.

              1 Reply Last reply
              2
              • J J.Hilk
                1 Mar 2021, 08:51

                @suslucoder surely you can think of at least one way to uniquify the file name automatically ?

                for example add the timestamp

                QFile file("/home/info." + QTime::currentTime().toString("mm.hh.ss.'txt'"));

                D Offline
                D Offline
                deleted286
                wrote on 1 Mar 2021, 10:53 last edited by
                #7

                @J-Hilk thank you

                1 Reply Last reply
                0

                6/7

                1 Mar 2021, 08:52

                • Login

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