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. how to create file if it not exist at give path in qt ?
Forum Updated to NodeBB v4.3 + New Features

how to create file if it not exist at give path in qt ?

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 3.6k Views 1 Watching
  • 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.
  • M mpergand
    14 Nov 2022, 11:29

    which option i need to choose for create file if it does not exist ?

    QFile::exists

    i need to choose for read and write combinedly using QFILE ?

    pass QIODevice::ReadWrite as mode in QFile::open(mode)
    QFile::open

    Q Offline
    Q Offline
    Qt embedded developer
    wrote on 14 Nov 2022, 11:35 last edited by
    #3

    @mpergand Hi QFile::exists will give information that file is exist or not. but how to create it when it's not there ?

    J 1 Reply Last reply 14 Nov 2022, 11:38
    0
    • Q Qt embedded developer
      14 Nov 2022, 11:35

      @mpergand Hi QFile::exists will give information that file is exist or not. but how to create it when it's not there ?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 14 Nov 2022, 11:38 last edited by jsulm
      #4

      @Qt-embedded-developer @mpergand gave you a socond link: what's wrong with it?

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

      Q 1 Reply Last reply 14 Nov 2022, 11:42
      0
      • J jsulm
        14 Nov 2022, 11:38

        @Qt-embedded-developer @mpergand gave you a socond link: what's wrong with it?

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on 14 Nov 2022, 11:42 last edited by Qt embedded developer
        #5

        @jsulm can you tell me with below written code

        QFile file("out.txt");
        file.open(QIODevice::ReadWrite)

        the file gets created or not ?

        because before reading or writing i have to create the file and this information is i think not clearly get from mentioned link.

        J 1 Reply Last reply 14 Nov 2022, 11:46
        0
        • Q Qt embedded developer
          14 Nov 2022, 11:42

          @jsulm can you tell me with below written code

          QFile file("out.txt");
          file.open(QIODevice::ReadWrite)

          the file gets created or not ?

          because before reading or writing i have to create the file and this information is i think not clearly get from mentioned link.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 14 Nov 2022, 11:46 last edited by
          #6

          @Qt-embedded-developer Why don't you read documentation? It answers your question (https://doc.qt.io/qt-5/qfile.html#open)...

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

          Q 1 Reply Last reply 14 Nov 2022, 11:48
          2
          • J jsulm
            14 Nov 2022, 11:46

            @Qt-embedded-developer Why don't you read documentation? It answers your question (https://doc.qt.io/qt-5/qfile.html#open)...

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on 14 Nov 2022, 11:48 last edited by Qt embedded developer
            #7
            This post is deleted!
            D J 2 Replies Last reply 14 Nov 2022, 11:50
            0
            • Q Qt embedded developer
              14 Nov 2022, 11:48

              This post is deleted!

              D Offline
              D Offline
              DerReisende
              wrote on 14 Nov 2022, 11:50 last edited by DerReisende
              #8

              @Qt-embedded-developer Thats not true:
              Note: In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.

              This is from the posted link

              J Q 2 Replies Last reply 14 Nov 2022, 11:51
              2
              • Q Qt embedded developer
                14 Nov 2022, 11:48

                This post is deleted!

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 14 Nov 2022, 11:50 last edited by jsulm
                #9

                @Qt-embedded-developer said in how to create file if it not exist at give path in qt ?:

                but for QIODevice::ReadWrite with this mode what happens is not mentioned

                I guess you mean ReadOnly? Well, if the documentation says the file will be created in case of WriteOnly and ReadWrite, then isn't it logical that the file will not be created in all other modes? Also, this is really easy to test.

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

                1 Reply Last reply
                0
                • D DerReisende
                  14 Nov 2022, 11:50

                  @Qt-embedded-developer Thats not true:
                  Note: In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.

                  This is from the posted link

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 14 Nov 2022, 11:51 last edited by
                  #10

                  @DerReisende said in how to create file if it not exist at give path in qt ?:

                  Thats not true

                  Really? How did you prove it is not true?

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

                  D 1 Reply Last reply 14 Nov 2022, 11:51
                  0
                  • J jsulm
                    14 Nov 2022, 11:51

                    @DerReisende said in how to create file if it not exist at give path in qt ?:

                    Thats not true

                    Really? How did you prove it is not true?

                    D Offline
                    D Offline
                    DerReisende
                    wrote on 14 Nov 2022, 11:51 last edited by
                    #11

                    @jsulm I was answering a deleted post from OP not yours.

                    J 1 Reply Last reply 14 Nov 2022, 11:52
                    0
                    • D DerReisende
                      14 Nov 2022, 11:51

                      @jsulm I was answering a deleted post from OP not yours.

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 14 Nov 2022, 11:52 last edited by
                      #12

                      @DerReisende Oh, sorry I was thinking I was replying to OP.

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

                      1 Reply Last reply
                      0
                      • Q Qt embedded developer
                        14 Nov 2022, 11:16

                        can anybody tell me which option i need to select for writing or updating into file which is not exist at given path. Because there may be situation when file may be not exist initially.

                        I want to know which option i need to choose for create file if it does not exist ?

                        i also want to know which option i need to choose for read and write combinedly using QFILE ?

                        J Offline
                        J Offline
                        JonB
                        wrote on 14 Nov 2022, 11:53 last edited by JonB
                        #13

                        @Qt-embedded-developer said in how to create file if it not exist at give path in qt ?:

                        i also want to know which option i need to choose for read and write combinedly using QFILE ?

                        I will say this: most of the time when people here open a QFile with QIODevice::ReadWrite it is unnecessary/wrong. What will you actually be doing which requires the file to be opened for read and write at the same time?

                        Q 1 Reply Last reply 15 Nov 2022, 04:19
                        0
                        • D DerReisende
                          14 Nov 2022, 11:50

                          @Qt-embedded-developer Thats not true:
                          Note: In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.

                          This is from the posted link

                          Q Offline
                          Q Offline
                          Qt embedded developer
                          wrote on 14 Nov 2022, 11:54 last edited by
                          #14

                          @jsulm means for first time if i want to create file then i must need to use WriteOnly or ReadWrite mode .

                          Okay thanks all. and sorry for deleting comment of mine before waiting for answer or updating the comment.

                          1 Reply Last reply
                          0
                          • J JonB
                            14 Nov 2022, 11:53

                            @Qt-embedded-developer said in how to create file if it not exist at give path in qt ?:

                            i also want to know which option i need to choose for read and write combinedly using QFILE ?

                            I will say this: most of the time when people here open a QFile with QIODevice::ReadWrite it is unnecessary/wrong. What will you actually be doing which requires the file to be opened for read and write at the same time?

                            Q Offline
                            Q Offline
                            Qt embedded developer
                            wrote on 15 Nov 2022, 04:19 last edited by
                            #15

                            @JonB can you elaborate some more. because in my case when i write any thing in file then

                            1] After first time i have to read file
                            2] i have to add update the content of file
                            3] then why this QIODevice::ReadWrite mode is unnecessary ?

                            J J 2 Replies Last reply 15 Nov 2022, 06:19
                            0
                            • Q Qt embedded developer
                              15 Nov 2022, 04:19

                              @JonB can you elaborate some more. because in my case when i write any thing in file then

                              1] After first time i have to read file
                              2] i have to add update the content of file
                              3] then why this QIODevice::ReadWrite mode is unnecessary ?

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 15 Nov 2022, 06:19 last edited by
                              #16

                              @Qt-embedded-developer said in how to create file if it not exist at give path in qt ?:

                              then why this QIODevice::ReadWrite mode is unnecessary ?

                              It is necessary if you want to create a new file and write to it.
                              But if you only want to read existing file or to only write into a file use ReadOnly/WriteOnly.

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

                              1 Reply Last reply
                              1
                              • Q Qt embedded developer
                                15 Nov 2022, 04:19

                                @JonB can you elaborate some more. because in my case when i write any thing in file then

                                1] After first time i have to read file
                                2] i have to add update the content of file
                                3] then why this QIODevice::ReadWrite mode is unnecessary ?

                                J Offline
                                J Offline
                                JonB
                                wrote on 15 Nov 2022, 10:59 last edited by JonB
                                #17

                                @Qt-embedded-developer said in how to create file if it not exist at give path in qt ?:

                                i have to add update the content of file

                                Here is the possibly dangerous issue. What exactly do you mean by "update"? Do you mean (a) replace certain bytes (b) overwrite whole file or (c) append to what is there? They all need different "open" code.

                                In your other thread you showed

                                QFile file(QDir::homePath() + "/Runtime/"+ "NewFile.txt");
                                
                                            if (!file.open(QIODevice::ReadWrite))
                                            {
                                            qDebug() << "file is not exist";
                                            qDebug()<< "Cannot open file for writing: "
                                                         << qPrintable(file.errorString());
                                            }
                                            else
                                            {
                                                QTextStream out(&file);
                                                out << secondsbetweenDateTime<< endl;
                                            }
                                

                                When the file does not already exist then (I believe) ReadWrite creates it anew at zero length. This path is OK. However, what about when it does exist, your else code? ReadWrite does not truncate the existing content and it does not append to it; rather, it just starts writing new bytes from the start "on top" of whatever happens to already be in the file. So say your existing file currently contains

                                Hello world
                                

                                Your code opens for read-write, say you write Bye to that file, and then you're done and close it. the file will now contain:

                                Byelo world
                                

                                Presumably not what you intended! This is why ReadWrite is rarely what people actually mean, certainly for a text file.

                                So if you want further help indicate what you really want to do with this file.

                                Q 1 Reply Last reply 17 Nov 2022, 11:19
                                0
                                • J JonB
                                  15 Nov 2022, 10:59

                                  @Qt-embedded-developer said in how to create file if it not exist at give path in qt ?:

                                  i have to add update the content of file

                                  Here is the possibly dangerous issue. What exactly do you mean by "update"? Do you mean (a) replace certain bytes (b) overwrite whole file or (c) append to what is there? They all need different "open" code.

                                  In your other thread you showed

                                  QFile file(QDir::homePath() + "/Runtime/"+ "NewFile.txt");
                                  
                                              if (!file.open(QIODevice::ReadWrite))
                                              {
                                              qDebug() << "file is not exist";
                                              qDebug()<< "Cannot open file for writing: "
                                                           << qPrintable(file.errorString());
                                              }
                                              else
                                              {
                                                  QTextStream out(&file);
                                                  out << secondsbetweenDateTime<< endl;
                                              }
                                  

                                  When the file does not already exist then (I believe) ReadWrite creates it anew at zero length. This path is OK. However, what about when it does exist, your else code? ReadWrite does not truncate the existing content and it does not append to it; rather, it just starts writing new bytes from the start "on top" of whatever happens to already be in the file. So say your existing file currently contains

                                  Hello world
                                  

                                  Your code opens for read-write, say you write Bye to that file, and then you're done and close it. the file will now contain:

                                  Byelo world
                                  

                                  Presumably not what you intended! This is why ReadWrite is rarely what people actually mean, certainly for a text file.

                                  So if you want further help indicate what you really want to do with this file.

                                  Q Offline
                                  Q Offline
                                  Qt embedded developer
                                  wrote on 17 Nov 2022, 11:19 last edited by Qt embedded developer
                                  #18

                                  @JonB IN MY CASE YOUR a AND b OPTION POSSIBLE

                                  I NEED TO DO LIKE BELOW:

                                  FILE CONTENT PREVIOUS :
                                  POWER ON TIME : 1000

                                  I HAVE TO READ ABOVE CONTENT AND CHANGE IT LIKE BELOW IF POWER ON TIME IS 12

                                  FILE CONTENT NOW:
                                  POWER ON TIME: 12

                                  J 1 Reply Last reply 17 Nov 2022, 12:07
                                  0
                                  • Q Qt embedded developer
                                    17 Nov 2022, 11:19

                                    @JonB IN MY CASE YOUR a AND b OPTION POSSIBLE

                                    I NEED TO DO LIKE BELOW:

                                    FILE CONTENT PREVIOUS :
                                    POWER ON TIME : 1000

                                    I HAVE TO READ ABOVE CONTENT AND CHANGE IT LIKE BELOW IF POWER ON TIME IS 12

                                    FILE CONTENT NOW:
                                    POWER ON TIME: 12

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 17 Nov 2022, 12:07 last edited by JonB
                                    #19

                                    @Qt-embedded-developer

                                    CHANGE IT

                                    And does that mean overwrite what is there or append to what is there? I would do one of:

                                    • If you do use QIODevice::ReadWrite and want to append I would go file.open(QIODevice::ReadWrite | QIODevice::Append), to ensure code does not overwrite what is already there.

                                    • If you want to overwrite after reading, then do not use QIODevice::ReadWrite. As I wrote above it will leave extra characters in the file if the new output is any shorter than the existing content. Instead do the next step....

                                    • Alternative is to open file ReadOnly, do reading, close. Then re-open for either overwrite/truncate or append and write the new stuff now.

                                    In all of above cases, if you are under Windows you should or in QIODeviceBase::Text since this is a text file.

                                    M 1 Reply Last reply 17 Nov 2022, 12:54
                                    1
                                    • J JonB
                                      17 Nov 2022, 12:07

                                      @Qt-embedded-developer

                                      CHANGE IT

                                      And does that mean overwrite what is there or append to what is there? I would do one of:

                                      • If you do use QIODevice::ReadWrite and want to append I would go file.open(QIODevice::ReadWrite | QIODevice::Append), to ensure code does not overwrite what is already there.

                                      • If you want to overwrite after reading, then do not use QIODevice::ReadWrite. As I wrote above it will leave extra characters in the file if the new output is any shorter than the existing content. Instead do the next step....

                                      • Alternative is to open file ReadOnly, do reading, close. Then re-open for either overwrite/truncate or append and write the new stuff now.

                                      In all of above cases, if you are under Windows you should or in QIODeviceBase::Text since this is a text file.

                                      M Offline
                                      M Offline
                                      mpergand
                                      wrote on 17 Nov 2022, 12:54 last edited by mpergand
                                      #20

                                      @JonB said in how to create file if it not exist at give path in qt ?:

                                      If you want to overwrite after reading, then do not use QIODevice::ReadWrite

                                      You can, simply do resize(0) before writing

                                      J 1 Reply Last reply 17 Nov 2022, 13:10
                                      0
                                      • M mpergand
                                        17 Nov 2022, 12:54

                                        @JonB said in how to create file if it not exist at give path in qt ?:

                                        If you want to overwrite after reading, then do not use QIODevice::ReadWrite

                                        You can, simply do resize(0) before writing

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 17 Nov 2022, 13:10 last edited by
                                        #21

                                        @mpergand
                                        Indeed, which probably calls truncate(). It does not say whether you must then seek(0) as well, many platforms require seek-when-change-from-read-to-write. It's yet another way. Personally I probably would not open for ReadWrite and do that. The OP now has a choice of approaches.

                                        1 Reply Last reply
                                        1

                                        12/21

                                        14 Nov 2022, 11:52

                                        • Login

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