linux c 创建多级目录函数实现 支持绝对路径和相对路径
来源:互联网 发布:傲剑神照经数据 编辑:程序博客网 时间:2024/06/05 20:35
#include <stdio.h>#include <unistd.h>#include <string.h>#include <sys/stat.h>/** * \function MakeDir * \author PengWeizhe * \date * \param [in] path 待创建的目录路径 可以相对路径和绝对路径 * \return 0 创建成功 1创建失败 * \details 创建一个目录(单级、多级) */int makeDir(const char* path){ int beginCmpPath; int endCmpPath; int fullPathLen; int pathLen = strlen(path); char currentPath[128] = {0}; char fullPath[128] = {0}; printf("path = %s\n", path); //相对路径 if('/' != path[0]) { //获取当前路径 getcwd(currentPath, sizeof(currentPath)); strcat(currentPath, "/"); printf("currentPath = %s\n", currentPath); beginCmpPath = strlen(currentPath); strcat(currentPath, path); if(path[pathLen] != '/') { strcat(currentPath, "/"); } endCmpPath = strlen(currentPath); } else { //绝对路径 int pathLen = strlen(path); strcpy(currentPath, path); if(path[pathLen] != '/') { strcat(currentPath, "/"); } beginCmpPath = 1; endCmpPath = strlen(currentPath); } //创建各级目录 for(int i = beginCmpPath; i < endCmpPath ; i++ ) { if('/' == currentPath[i]) { currentPath[i] = '\0'; if(access(currentPath, NULL) != 0) { if(mkdir(currentPath, 0755) == -1) { printf("currentPath = %s\n", currentPath); perror("mkdir error %s\n"); return -1; } } currentPath[i] = '/'; } } return 0;}int main(){ makeDir("/PWZ/HJY"); makeDir("helloworld"); makeDir("/home/test.txt"); return 0;}
1 0
- linux c 创建多级目录函数实现 支持绝对路径和相对路径
- C/C++创建多级目录函数,支持windows和linux
- C/C++创建多级目录函数,支持windows和linux
- 绝对路径和相对路径,创建目录和删除
- linux目录树和绝对路径与相对路径
- linux绝对路径和相对路径
- linux c 创建多级目录接口实现
- 相对路径和绝对路径HTML目录问题
- Linux C 创建多级目录
- Linux C 创建多级目录
- Objective-c中的相对路径和绝对路径
- c/c++中的相对路径和绝对路径
- c/c++中的相对路径和绝对路径
- linux 目录相对/绝对路径
- linux下的绝对路径和相对路径
- 相对路径和绝对路径
- 绝对路径和相对路径
- 相对路径和绝对路径
- python带给你的简单实用的http、ftp服务
- php.ini配置详细选项
- 【leetcode】438. Find All Anagrams in a String【E】
- 第八周项目5-计数的模式匹配
- centos配置history记录每个用户执行过的命令
- linux c 创建多级目录函数实现 支持绝对路径和相对路径
- POJ 2774 Long Long Message(后缀数组[最长公共子串])
- Mybatis 批处理
- VC++ 介绍值传递/指针传递/引用传递的作用
- Rect和RectF之间的区别和联
- 第九周项目四(广义表算法库)
- 第九周 对称矩阵压缩存储的实现
- java 多excel下载 打成zip压缩包 程序中创建文件 响应到浏览器
- 安卓Json和Gson