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. [Solved] How to create new folders directories from C++?
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to create new folders directories from C++?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.5k 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.
  • S Offline
    S Offline
    skammers
    wrote on 3 Jul 2014, 06:11 last edited by
    #1

    Hey!

    I need to create new folders from C++.
    I have tried this:

    @
    void masterResource::makeDirectories(QString folder)
    {
    QStringList fields = folder.split("///");
    folder = fields.at(1).trimmed();

    CreateDirectory("folder+"/Work", NULL);
    CreateDirectory(folder+"/InTouch", NULL);
    

    }
    @

    But then I get these errors:

    error: cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}' for argument '1' to 'WINBOOL CreateDirectoryW(LPCWSTR, LPSECURITY_ATTRIBUTES)'

    and

    error: cannot convert 'const QString' to 'LPCWSTR {aka const wchar_t*}' for argument '1' to 'WINBOOL CreateDirectoryW(LPCWSTR, LPSECURITY_ATTRIBUTES)'

    1 Reply Last reply
    0
    • S Offline
      S Offline
      skammers
      wrote on 3 Jul 2014, 06:16 last edited by
      #2

      Never mind, got it to work. The function was

      @CreateDirectoryA("folder+"/Work", NULL);@

      instead of

      @CreateDirectory("folder+"/Work", NULL);@

      Notice the 'A'.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andreyc
        wrote on 3 Jul 2014, 14:58 last edited by
        #3

        Another option is to use QDir
        @
        void masterResource::makeDirectories(QString folder)
        {
        QStringList fields = folder.split("///");
        folder = fields.at(1).trimmed();

        QDir currentDir;
        
        currentDir.mkpath(folder + "/Work");
        currentDir.mkpath(folder + "/InTouch");
        

        }
        @

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 3 Jul 2014, 22:20 last edited by
          #4

          All in all you should do what andreyc says and use portable Qt APIs, but if you won't then:

          Don't use A methods of winAPI. They are obsolete ANSI variant and don't handle national characters. You should use the W variants or the "no letter" macros (which get expanded to W variants when UNICODE is defined, which is the default these days). You need to provide a wide string for these, eg.
          @
          QString path = ...
          CreateDirectory(path.toStdWString().c_str());
          @
          If you use wide character string literals prepend them with letter L, eg. L"some string". That's how you define a wchar_t string literal in c++;

          Btw. your "folder" variable is a QString and the winAPI function takes a wchar_t pointer (LPCWSTR expands to that) so I don't see how your solution could work.

          1 Reply Last reply
          0

          1/4

          3 Jul 2014, 06:11

          • Login

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