You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C++中定位指定文件夹路径并在其中创建子文件夹

在C++中定位指定路径并创建文件夹

嘿,这事儿好办!我给你两种实用的实现方案,适配不同的场景,你按需挑选就行~

方案一:Windows原生API实现(仅Windows平台)

如果你只需要在Windows环境下运行程序,用Windows SDK自带的CreateDirectory函数最直接,不用额外装库。注意C字符串里的反斜杠要写成\\来转义,或者用C11的原始字符串R"(路径)"省得麻烦。

直接上代码:

#include <windows.h>
#include <iostream>

int main() {
    // 目标路径,这里用宽字符版本更好支持特殊字符路径
    const wchar_t* targetPath = L"C:\\Users\\218418\\Documents\\Visual Studio 2013\\Projects\\Netwokring Application\\Netwokring Application\\CENTRAL";
    
    // 调用CreateDirectoryW创建文件夹,NULL表示使用默认安全属性
    BOOL createResult = CreateDirectoryW(targetPath, NULL);
    
    if (createResult) {
        std::wcout << L"文件夹创建成功!" << std::endl;
    } else {
        DWORD errorCode = GetLastError();
        switch(errorCode) {
            case ERROR_ALREADY_EXISTS:
                std::wcout << L"文件夹已经存在,不用重复创建啦~" << std::endl;
                break;
            case ERROR_PATH_NOT_FOUND:
                std::wcout << L"上级路径不存在哦,得先确保所有父目录都已经创建好。" << std::endl;
                break;
            default:
                std::wcout << L"创建失败,错误码:" << errorCode << std::endl;
                break;
        }
    }
    return 0;
}

小提示:如果需要自动创建所有不存在的父目录,可以换成SHCreateDirectoryExW函数,它会递归创建路径上的所有缺失文件夹。

方案二:跨平台通用实现(C++17或Boost)

要是你想让程序能在Windows、Linux、Mac上都跑起来,推荐用标准库或者Boost的文件系统模块。

C++17标准库版本(VS2017及以上支持)

C++17加入了<filesystem>标准库,非常方便。代码如下:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
    // 用原始字符串写路径,不用转义反斜杠,省心
    fs::path targetPath = R"(C:\Users\218418\Documents\Visual Studio 2013\Projects\Netwokring Application\Netwokring Application\CENTRAL)";
    
    try {
        // create_directories会自动创建所有缺失的父目录,第二个参数控制权限
        bool isCreated = fs::create_directories(targetPath, fs::perms::all);
        if (isCreated) {
            std::cout << "文件夹创建成功!" << std::endl;
        } else {
            std::cout << "文件夹已经存在啦~" << std::endl;
        }
    } catch (const fs::filesystem_error& e) {
        std::cerr << "创建失败:" << e.what() << std::endl;
    }
    return 0;
}

VS2013适配方案(用Boost.Filesystem)

你现在用的是VS2013,它还不支持C++17的filesystem,那可以用Boost库的boost::filesystem替代。先安装好Boost,然后代码是这样的:

#include <boost/filesystem.hpp>
#include <iostream>

namespace fs = boost::filesystem;

int main() {
    fs::path targetPath = R"(C:\Users\218418\Documents\Visual Studio 2013\Projects\Netwokring Application\Netwokring Application\CENTRAL)";
    
    try {
        bool isCreated = fs::create_directories(targetPath);
        if (isCreated) {
            std::cout << "文件夹创建成功!" << std::endl;
        } else {
            std::cout << "文件夹已经存在啦~" << std::endl;
        }
    } catch (const fs::filesystem_error& e) {
        std::cerr << "创建失败:" << e.what() << std::endl;
    }
    return 0;
}

额外注意事项

  • 权限问题:要确保你的程序有足够的权限在目标路径下创建文件夹,比如有些系统目录需要管理员权限才能操作。
  • 路径拼写:你提到的Netwokring拼写错误,我已经原样保留在代码里啦,没问题~

内容的提问来源于stack exchange,提问作者jish Bailey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:17:20