#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>//determine if the directory bool Is_dir (const char *path) {struct stat statbuf; if (Lstat (path, &statbuf) ==0)//lstat returns information about the file, the file information is stored in the stat structure {return s_isdir (statbuf.st_mode)! = 0;//S_ISDIR macro To determine if the file type is directory} return false;} Determines whether the regular file bool Is_file (const char *path) {struct stat statbuf; if (Lstat (path, &statbuf) ==0) return S_isreg (statbuf.st_mode)! = 0;//Determine if the file is a regular file return false;} Determine if the special directory is bool Is_special_dir (const char *path) {return strcmp (path, ".") = = 0 | | strcmp (PATH, "..") = = 0;} Generate complete file path 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);} Delete all files under file directory 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); }}}//determine if the file exists bool Is_file_exist (const char *dir) {if (dir = = NULL) return false; if ((Access (DIR,F_OK))!=-1) {printf ("file exists \ n"); return true; } printf ("File does not exist \ n"); return false;} Determine if the file directory exists bool Is_dir_exist (const char *dir) {if (dir = = NULL) return false; if (Opendir (dir) = = NULL) {printf ("file directory does not exist \ n"); return false; } printf ("File directory exists \ n"); RetuRN true;} Create multilevel directory 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; Determine if the path exists, if it does not exist, create the path if (!is_dir_exist (DirName)) {if (mkdir (DirName, 0755) = =-1) { Perror ("mkdir error"); return false; }} Dirname[i] = '/'; }} return true; Create file to read/write file bool Create_file (const char *file_path, const char *file_name) {if (Is_dir_exist (File_path)) {//File already Existence if (Is_file_exist (file_name)) {return true; }} else{//file directory does not exist, create directory if (Create_dirs (File_path)) {//Create directory successfully, go here to create file if (open (File_name,o_rdwr | O_creat, S_IRUSR | S_irgrp | S_iroth)!=-1) return true; }} return false;} Delete file bool Delete_file (const char* file_path) {if (File_path = = NULL) {return false; } if (remove (file_path) ==-1) return false; return true; }//writes the file bool Write_file (const char *filename,const char *content) in the appended way {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;}Test all through under the Mac
Ios&android C + + file operations