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 4.0k 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.
  • Q Qt embedded developer

    This post is deleted!

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on 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

      @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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      0
      • jsulmJ jsulm

        @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 last edited by
        #11

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

        jsulmJ 1 Reply Last reply
        0
        • D DerReisende

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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

            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 ?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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
            0
            • D DerReisende

              @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 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
              • JonBJ JonB

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

                jsulmJ JonBJ 2 Replies Last reply
                0
                • Q Qt embedded developer

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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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
                    0
                    • JonBJ JonB

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

                      JonBJ 1 Reply Last reply
                      0
                      • Q Qt embedded developer

                        @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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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
                        1
                        • JonBJ JonB

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

                          JonBJ 1 Reply Last reply
                          0
                          • M mpergand

                            @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

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 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

                            • Login

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