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. Creating a file in c:\
Qt 6.11 is out! See what's new in the release blog

Creating a file in c:\

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.1k 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.
  • P Offline
    P Offline
    phil63
    wrote on last edited by
    #1

    Hi,

    With the code below we ceate a file in the debug file.
    How can I create the file in C:\ ?
    Thanks in advance for your help.

    @//creating a file named dossier donnes- if it doesn't exist
    QDir lDir;
    if (not lDir.exists("dossier donnes")) lDir.mkdir("dossier donnes");
    @

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Use a QFile to create files. Open the file, then write your data, then close the file. See http://qt-project.org/doc/qt-5.0/qtcore/qfile.html for more details

      @
      QFile output("myfile.txt");

      bool ok = output.open(QFile::WriteOnly|QFile::Text);
      if (ok)
      {
      output.write("Debug text");
      output.close();
      }
      else
      {
      qDebug("Could not open file!");
      }
      @

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • P Offline
        P Offline
        phil63
        wrote on last edited by
        #3

        Hi,
        Thank you for your reply.
        I badly formulated my question. Sorry about it.
        But my question was not 'how to create a file' but 'how to create a directory file' at a certain location'
        debug is a directory file created by QTCreator when compiling a file . It contains beside others the .exe program.
        'dossier donnes' is a directory file I want to create in C:\ .
        And in this directory file (dossier donnes) I want to create in a next step, a txt file.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          The constructor allows you to provide a starting directory, "see QDir.":http://qt-project.org/doc/qt-5.0/qtcore/qdir.html#QDir-2 Since you did not specify a string it assigns an empty one. I am not sure where this points, but you know already where to start, so
          @
          //creating a file named dossier donnes- if it doesn't exist
          QDir lDir ( "c:/" );
          if (not lDir.exists("dossier donnes")) lDir.mkdir("dossier donnes");
          @
          should work.

          Alternatively, you may use "mkpath":http://qt-project.org/doc/qt-5.0/qtcore/qdir.html#mkpath which allows to create cascading directories at once.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            Sorry, just saw the "not" in the if statement, what compile language are you using?
            In C++ it would be

            @
            //creating a file named dossier donnes- if it doesn't exist
            QDir lDir ( "c:/" );
            if ( ! lDir.exists("dossier donnes")) lDir.mkdir("dossier donnes");
            @

            or

            @
            //creating a file named dossier donnes- if it doesn't exist
            QDir lDir ( "c:/" );
            if ( ! lDir.exists("c:/dossier donnes/dossier donnes")) lDir.mkpath("c:/dossier donnes/dossier donnes");
            @

            Note: you may use exists for absolute path already.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • P Offline
              P Offline
              phil63
              wrote on last edited by
              #6

              Thank you for your help.

              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