open surce
open surce

Reputation: 55

Create a structure of folders

I'm trying to create a folder structure, for example something like this:

c:\game\user\user_profile\...

But when I'm using the CreateDirectory() function, it doesn't seem to be doing what I want it to do. I imagine I'm using it incorrectly, and I would really appreciate a quick explanation on what the function is actually doing. Here's my code:

void CreateDir(const char* path) {

    if (!CreateDirectory(path, NULL))
    {
        cout << "Fail";
        return;
    }
    else
        cout << "Success?";
}

int main()
{
    CreateDir("c:\\game\\user\\user_profile");
    system("pause");
}

It seems that the function allows me to create a single folder fine (game), and then allows me to add 1 more folder inside it (user - I assume it's because it knows where game is), but if I try to include more than 1 folder to the directory, it seems to fail.

I want to be able to create a structure of folders using this function, but it doesn't seem to work.

Again I'm sure I'm using this function incorrectly here, could someone advise?

Upvotes: 0

Views: 311

Answers (1)

Mert K&#246;kl&#252;
Mert K&#246;kl&#252;

Reputation: 2231

If you are using C++ 17 filesystem library is part of the standart library so you can use it like this:

#include <filesystem>

std::filesystem::create_directories("c:\\game\\user\\user_profile");

And if you are using g++ you need to add "-std=c++17" and "-lstdc++fs" flags.

Upvotes: 1

Related Questions