標籤:
#include <sys/stat.h>#include <unistd.h>#include <stdlib.h>#include <dirent.h>#include <limits.h>#include <string.h>#include <stdio.h>#include <limits.h>#include<sys/types.h>#include<fcntl.h>#include <assert.h>//判斷是否為目錄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_dirs_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_dirs_file(file_path); rmdir(file_path); } }}//判斷檔案是否存在bool is_file_exist(const char *dir) { if (dir == NULL) return false; if ((access(dir,F_OK))!=-1){ printf(" 檔案存在\n"); return true; } printf(" 檔案不存在\n"); return false;}//判斷檔案目錄是否存在bool is_dir_exist(const char *dir) { if (dir == NULL) return false; if (opendir(dir) == NULL) { printf(" 檔案目錄不存在\n"); return false; } printf(" 檔案目錄存在\n"); return true;}//建立多級目錄bool create_dirs(const char *dir) { if (dir == NULL) return false; char DirName[256]; strcpy(DirName, dir); int i, len = strlen(DirName); if (DirName[len - 1] != '/') strcat(DirName, "/"); len = strlen(DirName); for (i = 1; i < len; i++) { if (DirName[i] == '/') { DirName[i] = 0; //判斷路徑是否存在,如果不存在則建立路徑 if (!is_dir_exist(DirName) ) { if (mkdir(DirName, 0755) == -1) { perror("mkdir error"); return false; } } DirName[i] = '/'; } } return true;}//建立檔案可讀寫的檔案bool create_file(const char *file_path, const char *file_name) { if(is_dir_exist(file_path)){ //檔案已經存在 if (is_file_exist(file_name)) { return true; } } else{ //檔案目錄不存在,建立目錄 if (create_dirs(file_path)) { // 建立目錄成功,這裡去建立檔案 if(open(file_name,O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH)!=-1) return true; } } return false;}//刪除檔案bool delete_file(const char* file_path) { if (file_path == NULL) { return false; } if(remove(file_path)==-1) return false; return true; }//以追加的方式寫入檔案bool write_file(const char *fileName,const char *content) { FILE *fp; fp = fopen(fileName, "a+"); if (NULL == fp) { return false; } if( fwrite(content, strlen(content), 1, fp)!=1) return false; fclose (fp); return true;}int main(int argc, char **argv){ // create_dirs("/Users/sunger/desktop/sunger/a/b/c"); is_file_exist("/Users/sunger/desktop/a.c"); read_file("/Users/sunger/desktop/a.c"); return 0;}在mac下測試全部通過
ios&android c++檔案操作