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;
}

参考资料:

相关推荐
喵了meme20 分钟前
C语言实战5
c语言·开发语言
神仙别闹2 小时前
基于C语言实现B树存储的图书管理系统
c语言·前端·b树
福尔摩斯张3 小时前
C++核心特性精讲:从C语言痛点出发,掌握现代C++编程精髓(超详细)
java·linux·c语言·数据结构·c++·驱动开发·算法
小尧嵌入式6 小时前
C语言中的面向对象思想
c语言·开发语言·数据结构·c++·单片机·qt
一杯美式 no sugar6 小时前
数据结构——单向无头不循环链表
c语言·数据结构·链表
南棱笑笑生7 小时前
20251215给飞凌OK3588-C开发板适配Rockchip原厂的Buildroot【linux-5.10】后调通typeC1接口
linux·c语言·开发语言·rockchip
star learning white7 小时前
xm C语言12
服务器·c语言·前端
超级大福宝7 小时前
C++中1 << 31 - 1相当于INT_MAX吗?
c语言·c++
芯联智造7 小时前
【stm32简单外设篇】- 高灵敏麦克风传感器模块 KY-037
c语言·stm32·单片机·嵌入式硬件
foundbug9998 小时前
Modbus协议C语言实现(易于移植版本)
java·c语言·前端