| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2102 人关注过本帖
标题:我写了一段C代码进行递归删除文件和文件夹,文件是删除了,但是文件夹删除失 ...
只看楼主 加入收藏
songhuirong1
Rank: 2
等 级:论坛游民
帖 子:116
专家分:38
注 册:2010-6-15
结帖率:94.12%
收藏
已结贴  问题点数:20 回复次数:3 
我写了一段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);
}

搜索更多相关主题的帖子: 文件夹 color 
2011-12-14 22:55
共和国鹰派
Rank: 3Rank: 3
来 自:山东
等 级:论坛游侠
帖 子:37
专家分:119
注 册:2011-10-20
收藏
得分:10 
就我所知文件夹是不能删除的,即使在dos下要使用命令删除文件夹也是需要确认信息的,你好像用的turbo c吧?这个我没用过,不过文件夹一般是不可直接删除
2011-12-15 00:14
我是菜鸟C
Rank: 4
等 级:业余侠客
帖 子:74
专家分:200
注 册:2011-3-27
收藏
得分:10 
rmdir只能删除空的文件夹吧???非空的删除不了啊貌似。。。
2011-12-15 10:00
songhuirong1
Rank: 2
等 级:论坛游民
帖 子:116
专家分:38
注 册:2010-6-15
收藏
得分:0 
哈哈。我已经解决了,在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);
}
2011-12-15 20:26
快速回复:我写了一段C代码进行递归删除文件和文件夹,文件是删除了,但是文件夹 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017394 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved