C语言删除文件夹下的目录

来源:互联网 发布:程序员手机壁纸1080 编辑:程序博客网 时间:2024/04/28 00:59

前几天使用Android反编译工具反编译了360 的软件,Xml文件不可以用,但是每个xml都对应的编译出了一个.txt文件

恢复的办法就是删除.xml然后重命名.txt文件,文件很多 打算写个小工具,想着尝试用C 语言写下,中间还是遇到了些问题

记录下来了.

/* ============================================================================ Name        : studyc.c Author      : popo Version     : Copyright   : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include <stdio.h>#include <dirent.h>#include <string.h>#include <errno.h>//显示删除失败原因void show_error(const char *file_name ){    switch(errno)    {        case ENOTEMPTY:            printf("Given path is not a directory, the directory is not empty, or the directory is either the current working directory or the root directory.\n");            break;        case ENOENT:            printf("%s Path is invalid.\n",file_name);            break;        case EACCES:            printf("%s had been opend by some application, can't delete.\n", file_name);            break;    }}int main(void) {printf("\n");DIR * dir;char fileName[300];char * fileType;int len;int delrs;int i;struct dirent * sonDir;if((dir=opendir("E:\\软件\\360MobileSafe_2.9.7beta\\res\\layout\\"))!=NULL){//if((dir=opendir("./"))!=NULL){printf("dir=%s\n",dir->dd_name);while((sonDir =readdir(dir))!=NULL){printf("sonDir=%s\n",sonDir->d_name);for(i=sonDir->d_namlen-1;i>=0;i--){if(sonDir->d_name[i]=='.'){//指针真是个好东西 不用再内存拷贝了fileType=&(sonDir->d_name[i]);break;}}//有后缀if(i>=0){if(strcmp(".txt",fileType)==0){//需要使用绝对路径删除 我刚开始删除的时候用的相对路径 结果不成功//使用show_error 后发现是路径不可用  才恍然大悟strcpy(fileName,dir->dd_name);fileName[strlen(fileName)-1]='\0';strcat(fileName,sonDir->d_name);delrs =remove(fileName);//删除失败打印错误信息if(delrs==-1)show_error(fileName);printf("we should delete the %s res=%d\n",fileName,delrs);}else{printf("we can not delete the %s \n",fileName);}}}}else{printf("file open fail");}//getchar();return 0;}

这里这是删除文件部分 继续完成 重命名部分已经 递归完成子目录功能


原创粉丝点击