注册 登录
编程论坛 C++教室

我写了一段C代码进行递归删除文件和文件夹,文件是删除了,但是文件夹删除失败!

songhuirong1 发布于 2011-12-14 22:55, 2102 次点击
我写了一段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);
}

3 回复
#2
共和国鹰派2011-12-15 00:14
就我所知文件夹是不能删除的,即使在dos下要使用命令删除文件夹也是需要确认信息的,你好像用的turbo c吧?这个我没用过,不过文件夹一般是不可直接删除
#3
我是菜鸟C2011-12-15 10:00
rmdir只能删除空的文件夹吧???非空的删除不了啊貌似。。。
#4
songhuirong12011-12-15 20:26
哈哈。我已经解决了,在while循环后面加一条代码就ok了!_findclose(handle);哎。忘记关闭了,当然删除失败,现在可以正常删除了。
现在贴出正确的代码:
程序代码:
#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);
            }
            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);

    //添加的新代码
    _findclose(handle);
}

void main()
{
    char *filepath = "G:\\ttt";
    delFiles(filepath);

    if(_rmdir(filepath) == 0)
        printf("删除目录%s成功!\n",filepath);
    else
        printf("删除目录%s失败!\n",filepath);
}
1