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 creating new file to designated path
Forum Updated to NodeBB v4.3 + New Features

How to creating new file to designated path

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 13.4k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #2

    Hi
    If you mean you want to create any folder in the path given that do not exists then no
    as far as i know.
    But you can use
    http://doc.qt.io/qt-5/qdir.html#mkpath
    to make the full path and then QFile to save the file.

    V 1 Reply Last reply
    4
    • V victor wang

      Hi All,
      I'm using linux system.
      Now I want to create a file in my USB directory.
      I was trying to see if QFile can do what I want.
      But it seems can only create file in root path or home path.

      Is there any way to create a new file to where I designated?

      Thanks in Advanced!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @victor-wang said in How to creating new file to designated path:

      But it seems can only create file in root path or home path.

      That's wrong, why do you think so? You can store files wherever you have write access. Just give QFile the path.
      What is your actual problem?
      USB storage is usually mounted in /media/USB_DEVICE_NAME on Linux.

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

      V 1 Reply Last reply
      4
      • jsulmJ jsulm

        @victor-wang said in How to creating new file to designated path:

        But it seems can only create file in root path or home path.

        That's wrong, why do you think so? You can store files wherever you have write access. Just give QFile the path.
        What is your actual problem?
        USB storage is usually mounted in /media/USB_DEVICE_NAME on Linux.

        V Offline
        V Offline
        victor wang
        wrote on last edited by
        #4

        @jsulm said in How to creating new file to designated path:

        @victor-wang said in How to creating new file to designated path:

        But it seems can only create file in root path or home path.

        That's wrong, why do you think so? You can store files wherever you have write access. Just give QFile the path.
        What is your actual problem?
        USB storage is usually mounted in /media/USB_DEVICE_NAME on Linux.

        Hi,
        Thanks for your reply.
        Here is my current code.

        QFile filepath;
        filepath.setFileName("/run/media/usb");
        if(filepath.exist())
        {
                 qDebug("file is exist");
        }
        else
        {
                 qDebug("create new file");
                 
        }
        
        

        Now I could not know which I have to use.
        Below command is to create a Folder.

        filepath.mkdir()
        

        But I want to create a File.
        I do not know which function that I can use to create a file not folder.

        can you teach me which one I should use?

        1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          If you mean you want to create any folder in the path given that do not exists then no
          as far as i know.
          But you can use
          http://doc.qt.io/qt-5/qdir.html#mkpath
          to make the full path and then QFile to save the file.

          V Offline
          V Offline
          victor wang
          wrote on last edited by
          #5

          @mrjj said in How to creating new file to designated path:

          Hi
          If you mean you want to create any folder in the path given that do not exists then no
          as far as i know.
          But you can use
          http://doc.qt.io/qt-5/qdir.html#mkpath
          to make the full path and then QFile to save the file.

          Actually I want to create a File that does not exist under the exist path.
          For instance, there is a folder "/run/media/usb" and I want to create a file that does not exist and call it test.txt.

          I'm not sure that QFile can complete it or not.
          Cause there is no test.txt then QFile could not open it.

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Hi

             QString filename = "/run/media/usb/test.txt";
                QFile file(filename);
                if (file.open(QIODevice::ReadWrite)) {
                    QTextStream stream(&file);
                    stream << "something" << endl;
                } else
                qDebug << "file open error",
            }
            
            
            V 1 Reply Last reply
            2
            • J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #7

              a slight additon to @mrjj
              IIRC than QFile can not create subdirectories. So make sure the target folder actually exists, and if it doesn't use mkdir beforehand.


              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.

              aha_1980A 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                a slight additon to @mrjj
                IIRC than QFile can not create subdirectories. So make sure the target folder actually exists, and if it doesn't use mkdir beforehand.

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @J.Hilk I'm pretty sure these restrictions apply to every concept of file. It's like a car that can only drive on a road that already exists.

                Qt has to stay free or it will die.

                1 Reply Last reply
                0
                • mrjjM mrjj

                  Hi

                   QString filename = "/run/media/usb/test.txt";
                      QFile file(filename);
                      if (file.open(QIODevice::ReadWrite)) {
                          QTextStream stream(&file);
                          stream << "something" << endl;
                      } else
                      qDebug << "file open error",
                  }
                  
                  
                  V Offline
                  V Offline
                  victor wang
                  wrote on last edited by victor wang
                  #9

                  @mrjj said in How to creating new file to designated path:

                  Hi

                   QString filename = "/run/media/usb/test.txt";
                      QFile file(filename);
                      if (file.open(QIODevice::ReadWrite)) {
                          QTextStream stream(&file);
                          stream << "something" << endl;
                      } else
                      qDebug << "file open error",
                  }
                  
                  

                  Thank you so much for your example!

                  I just google for the answer here.
                  https://stackoverflow.com/questions/3024382/how-to-create-a-new-file-with-full-path-in-qt

                  It explains that QFile will check if your file exist or not, if not it will create a new one then open it!

                  Here is my new code.

                  int CreateFile::create(QString filePath)
                  {
                      QFile file;
                      file.setFileName(filePath);
                      if(file.open(QIODevice::WriteOnly|QIODevice::Text))
                      {
                          qDebug("Create "+filePath+" Success!");
                      }
                      else
                      {
                          qDebug("Create "+filePath+" Filed!");
                          return false;
                      }
                      file.close();
                      if(file.exists())
                      {
                          qDebug(filePath+"exist");
                          return true;
                      }
                      else
                          return false;
                  
                  }
                  

                  For additional question, are there any ways to report the error from system when file could not normally be created?

                  mrjjM 1 Reply Last reply
                  0
                  • V victor wang

                    @mrjj said in How to creating new file to designated path:

                    Hi

                     QString filename = "/run/media/usb/test.txt";
                        QFile file(filename);
                        if (file.open(QIODevice::ReadWrite)) {
                            QTextStream stream(&file);
                            stream << "something" << endl;
                        } else
                        qDebug << "file open error",
                    }
                    
                    

                    Thank you so much for your example!

                    I just google for the answer here.
                    https://stackoverflow.com/questions/3024382/how-to-create-a-new-file-with-full-path-in-qt

                    It explains that QFile will check if your file exist or not, if not it will create a new one then open it!

                    Here is my new code.

                    int CreateFile::create(QString filePath)
                    {
                        QFile file;
                        file.setFileName(filePath);
                        if(file.open(QIODevice::WriteOnly|QIODevice::Text))
                        {
                            qDebug("Create "+filePath+" Success!");
                        }
                        else
                        {
                            qDebug("Create "+filePath+" Filed!");
                            return false;
                        }
                        file.close();
                        if(file.exists())
                        {
                            qDebug(filePath+"exist");
                            return true;
                        }
                        else
                            return false;
                    
                    }
                    

                    For additional question, are there any ways to report the error from system when file could not normally be created?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @victor-wang
                    Hi
                    there is
                    http://doc.qt.io/qt-5/qfiledevice.html#error

                    V 1 Reply Last reply
                    3
                    • mrjjM mrjj

                      @victor-wang
                      Hi
                      there is
                      http://doc.qt.io/qt-5/qfiledevice.html#error

                      V Offline
                      V Offline
                      victor wang
                      wrote on last edited by
                      #11

                      @mrjj said in How to creating new file to designated path:

                      @victor-wang
                      Hi
                      there is
                      http://doc.qt.io/qt-5/qfiledevice.html#error

                      Thanks alot !
                      I will look into it!

                      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