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

write to a different file each time of run



  • 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();
    
    }
    


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



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


  • Lifetime Qt Champion

    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 ?


  • Moderators

    @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'"));



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



  • @J-Hilk thank you


Log in to reply