C语言创建文件夹和多级目录

C调用系统命令创建多级目录

c 复制代码
#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *path = "a/b/c";

    // 创建目录命令的字符串
    char mkdir_command[100];
    sprintf(mkdir_command, "mkdir %s", path);

    // 调用系统命令
    system(mkdir_command);

    return 0;
}

注意,如果是在windows下,路径要改成 "a\b\c" 要实现跨平台可以写一个更改分隔符的实现。

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
#define PATH_SEPARATOR '\\'
#else
#define PATH_SEPARATOR '/'
#endif

int main() {
    char path[100]= "a/b/c";
    for (int i = 0; path[i]; i++) {
        if (path[i] == '/') {
            path[i] = PATH_SEPARATOR;
        }
        
    }
    printf("Path: %s\n", path);

    // 创建目录命令的字符串
    char mkdir_command[100];
    sprintf(mkdir_command, "mkdir %s", path);

    // 调用系统命令
    system(mkdir_command);

    puts("End");
    return 0;
}

windows下mkdir默认就支持多级目录

linux下mkdir需要参数p支持多级目录

如果文件已存在,命令行会显示子目录或文件已存在,但这又不是错误,不会影响后面的执行,所以无需手动判断该路径是否存在。

用C创建文件夹

用C来创建文件夹不是很推荐,因为他默认不支持多级目录。而且创建文件夹一般是项目初始化的过程,对性能也没什么要求,用C来实现很繁琐。

linux下使用 <sys/stat.h> 头文件中的 mkdir() 函数来创建目录。

c 复制代码
#include <stdio.h>
#include <sys/stat.h>

int main() {
    char* dirname = "my_directory"; // 要创建的目录名称

    // 使用 mkdir() 函数创建目录
    if (mkdir(dirname, 0777) == 0) {
        printf("目录创建成功: %s\n", dirname);
    } else {
        printf("无法创建目录: %s\n", dirname);
    }

    return 0;
}

windows下使用 <direct.h> 头文件中的 _mkdir() 函数来创建目录。

c 复制代码
#include <stdio.h>
#include <direct.h>

int main() {
    char* dirname = "my_directory"; // 要创建的目录名称

    // 使用 _mkdir() 函数创建目录
    if (_mkdir(dirname) == 0) {
        printf("目录创建成功: %s\n", dirname);
    } else {
        printf("无法创建目录: %s\n", dirname);
    }

    return 0;
}

C创建多级文件夹的实现

方法一:函数递归

c 复制代码
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>

// 递归创建目录
void createDirectory(const char *path) {
    struct stat st;
    // 如果目录已存在,则不执行创建操作
    if (stat(path, &st) == 0) {
        printf("Directory %s already exists.\n", path);
        return;
    }
    
    // 递归创建上一级目录
    createDirectory(dirname(path));
    
    // 创建当前目录
    int status = mkdir(path, 0700); // 0700 权限可根据需要更改
    if (status == 0) {
        printf("Directory %s created.\n", path);
    } else {
        printf("Failed to create directory %s.\n", path);
    }
}

int main() {
    // 要创建的目录路径
    const char *path = "/path/to/your/directory";
    
    // 调用函数创建目录
    createDirectory(path);
    
    return 0;
}

递归的方式总觉得对性能不太友好,另外还有2个方案。

较笨的方法是使用strtok函数以及strcat函数来逐步分割和构建路径。

取巧的方法是使用strchr获取/位置,然后逐步构建路径

方法2:strchr实现逐步构建路径

c 复制代码
#include <stdio.h>
#include <string.h>
#include <direct.h>
#include <errno.h>
int mkdirr(char *path) {
    // 接下来要以/来确定要创建的目录,所以最后必须是/结尾,否则最后这个就不会创建
    if (path[strlen(path) - 1] != '/') {
        strcat(path, "/");
    }
    // puts(path);
    char *dir = path;
    while (dir = strchr(dir, '/')) {
        // printf("Found '/' at position %ld\n", dir - path);
        *dir = '\0';
        // printf("path: %s\n", path);
        if (_mkdir(path) == 0 || errno == EEXIST) {
            printf("Directory \"%s\" created successfully or already exists.\n", path);
        }
        *dir = '/';
        dir++;
    }
}
int main() {
    char path[] = "test/1/2/3";
    mkdirr(path);
    return 0;
}

参考资料:

相关推荐
czy87874751 小时前
C语言主要标准版本的演进与核心区别的对比分析
c语言
巨龙之路1 小时前
C语言中的assert
c语言·开发语言
吃个早饭7 小时前
2025年第十六届蓝桥杯大赛软件赛C/C++大学B组题解
c语言·c++·蓝桥杯
qwertyuiop_i8 小时前
pe文件二进制解析(用c/c++解析一个二进制pe文件)
c语言·c++·pe文件
我叫珂蛋儿吖10 小时前
[redis进阶六]详解redis作为缓存&&分布式锁
运维·c语言·数据库·c++·redis·分布式·缓存
周Echo周10 小时前
20、map和set、unordered_map、un_ordered_set的复现
c语言·开发语言·数据结构·c++·算法·leetcode·list
安装虚拟机的老师傅11 小时前
【2025最新】Windows系统装VSCode搭建C/C++开发环境(附带所有安装包)
c语言·windows·vscode·其他
真的想上岸啊12 小时前
c语言第一个小游戏:贪吃蛇小游戏06
c语言·算法·链表
hardStudy_h12 小时前
C程序的存储空间分配
c语言·开发语言
h汉堡13 小时前
C/C++内存管理
java·c语言·开发语言·c++·学习