c/c++创建删除文件 文件夹
发布日期:2021-06-28 22:17:15
浏览次数:2
分类:技术文章
本文共 1545 字,大约阅读时间需要 5 分钟。
//创建新目录 bool create_dir(const char*dirName){ if (FileUtils::getInstance()->isFileExist(dirName)) { return true; }else{ if (mkdir(dirName, 00700)<0) { return false; }else{ return true; } } } //判断是否为目录 bool is_dir(const char *path) { struct stat statbuf; if(lstat(path, &statbuf) ==0)//lstat返回文件的信息,文件信息存放在stat结构中 { return S_ISDIR(statbuf.st_mode) != 0;//S_ISDIR宏,判断文件类型是否为目录 } return false; } //判断是否为常规文件 bool is_file(const char *path) { struct stat statbuf; if(lstat(path, &statbuf) ==0) return S_ISREG(statbuf.st_mode) != 0;//判断文件是否为常规文件 return false; } //判断是否是特殊目录 bool is_special_dir(const char *path) { return strcmp(path, ".") == 0 || strcmp(path, "..") == 0; } //生成完整的文件路径 void get_file_path(const char *path, const char *file_name, char *file_path) { strcpy(file_path, path); if(file_path[strlen(path) - 1] != '/') strcat(file_path, "/"); strcat(file_path, file_name); } void delete_file(const char *path) { DIR *dir; dirent *dir_info; char file_path[PATH_MAX]; if(is_file(path)) { remove(path); return; } if(is_dir(path)) { if((dir = opendir(path)) == NULL) return; while((dir_info = readdir(dir)) != NULL) { get_file_path(path, dir_info->d_name, file_path); if(is_special_dir(dir_info->d_name)) continue; delete_file(file_path); rmdir(file_path); } } } //创建新文件 bool create_file(const char*fileName){ if(FileUtils::getInstance()->isFileExist(fileName)){ return false; }else{ if(creat(fileName,00700)<0) { printf("create file %s failure!\n",fileName); return true; } else { printf("create file %s sucess!\n",fileName); return false; } } }转载地址:https://blog.csdn.net/yhhwatl/article/details/46530429 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月29日 09时14分43秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
AS-->如何更高效的使用 Gradle, 快速build apk
2019-04-29
Android-->Android 原生支持圆角图片,圆角ImageView
2019-04-29
Android-->UDP协议的Socket数据传输
2019-04-29
Android-->Rxjava与Retrofit2的结合实战
2019-04-29
Android-->iostat(显示CPU和IO系统负载情况)
2019-04-29
Android-->Realm(数据库ORM)使用体验,lambda表达式
2019-04-29
OS--›Git操作演练(项目实用命令)
2019-04-29
Android-->Log系统,Logback的使用体验(slf4j)
2019-04-29
Ndk-->通过Javah生成.h头文件(Jni入门之路)
2019-04-29
Android-->自定义模版EditText(多用于手机号码,银行卡号等输入)
2019-04-29
Android-->IPC(跨进程)通信Aidl的实现方法
2019-04-29
Android-->电话广播的监听
2019-04-29
Bluetooth-->蓝牙开发之状态判断
2019-04-29