我写了一段C代码进行递归删除文件和文件夹,文件是删除了,但是文件夹删除失败!
我写了一段C代码进行递归删除某个文件夹下所有的文件和文件夹,所有文件是被删除了,但是文件夹却删除失败。请各位高手解答!谢谢!程序代码:
#include <stdio.h> #include <string.h> #include <io.h> #include <direct.h> #include <sys/stat.h> #include <process.h> #define MAXPATH 256 void delFiles(char *filepath) { long handle; long result; struct _finddata_t fileinfo; struct _stat stat; char path[MAXPATH]; char file[MAXPATH]; char *str; str = "\\*.*"; strcpy(path,filepath); strcat(path,str); handle = _findfirst(path,&fileinfo); if(handle == -1) { printf("Read file error!\n"); return; } do { char *s = fileinfo.name; if(*s == '.') continue; strcpy(file,filepath); strcat(file,"\\"); strcat(file,s); result = _stat(file,&stat); if(result != 0) { printf("Get file info error!\n"); return; } switch(stat.st_mode & S_IFMT) { case S_IFDIR: { char temp[1024]; delFiles(file); if(_rmdir(file) == 0) printf("删除目录%s成功!\n",file); else printf("删除目录%s失败!\n",file); /*sprintf(temp,"rmdir %s",file); system(temp);*/ } break; case S_IFREG: { if(remove(file) == 0) printf("删除文件%s成功!\n",file); else printf("删除文件%s失败!\n",file); } break; default: break; } } while(_findnext(handle,&fileinfo) != -1); } void main() { char *filepath = "G:\\ttt"; delFiles(filepath); }