写的一段创建文件夹的代码c++

// makenewfile.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
    string folderPath = "D:\\database\\Genshin1111";

    if (!GetFileAttributesA(folderPath.c_str()) && FILE_ATTRIBUTE_DIRECTORY) {
        bool flag = CreateDirectory(folderPath.c_str(), NULL);
        // flag 为 true 说明创建成功
    }
    else {
        cout << "Directory already exists." << endl;
    }
    return 0;
}



在论坛上抄写的一段创建文件夹的代码,但是怎么运行都是Directory already exists
求解答

d盘是不是已经有了 D:\database\Genshin1111 这个目录

你的判断是错误的 ,
GetFileAttributesA 如果文件不存在,返回0xffffffff ,执行逻辑非,则是false,结果就是执行下一个分支
应该

if (!(GetFileAttributesA(folderPath.c_str()) & FILE_ATTRIBUTE_DIRECTORY)) {
        bool flag = CreateDirectory(folderPath.c_str(), NULL);
        // flag 为 true 说明创建成功
    }