C语言自编文件/文件夹处理函数——文件/文件夹 创建/删除/列表

使用方法:将.c和.cpp文件添加到你的项目中,然后#include “FileDirTool.h”

#include <stdio.h>

#include "FileDirTools.h"

int main()
{
//    //listDir使用示例
//    char dirList[50][20];
//    int dirCnt = listDir("Database", dirList);
//    for (int i = 0; i < dirCnt; ++i)
//        printf("%s\n", dirList[i]);

//    //createDir使用示例
//    int crF = createDir("Database");
//    printf("%d\n", crF);

//    //dropDir使用
//    int drF = dropDir("Database");
//    printf("%d\n", drF);

//    //cascadeDropDir使用
//    cascadeDropDir("Database");



//    //listFile使用示例
//    char fileList[50][20];
//    int fileCnt = listFile("Database", fileList);
//    for (int i = 0; i < fileCnt; ++i)
//        printf("%s\n", fileList[i]);

//    //createFile使用示例
//    int crF = createFile("Database\\test.txt");
//    printf("%d\n", crF);

//    //dropFile使用示例
//    int drF = dropFile("Database\\test.txt");
//    printf("%d\n", drF);

    return 0;
}
//FileDirTools.h


#ifndef FILEDIRTOOLS_H_INCLUDED
#define FILEDIRTOOLS_H_INCLUDED

#include <direct.h>
#include <io.h>
#include <malloc.h>
#include <string.h>

//在指定路径下创建文件夹
//返回值   0创建成功 1文件夹已存在 -1不存在且创建失败
int createDir(char* path);

//在指定路径下删除文件夹(里面有内容将失败返回-1)
//返回值   0删除成功 1文件夹不存在 -1存在且删除失败
int dropDir(char* path);

//级联删除指定路径下的文件夹以及文件夹下的普通文件
void cascadeDropDir(char* path);

//返回指定路径下的所有文件夹名(上限50个目录 长度20)
//path路径   dirList文件夹名列表  返回值为文件夹名列表长度
int listDir(char *path, char dirList[][20]);

//创建文件 成功0失败-1
int createFile(char *path);

//删除文挨 成功0失败-1
int dropFile(char *path);

//返回指定路径下的所有文件名(上限50个文件 长度20)
//path路径   fileList文件名列表  返回值为文件名列表长度
int listFile(char *path, char fileList[][20]);

#endif // FILEDIRTOOLS_H_INCLUDED
//FileDirTools.c


#include "FileDirTools.h"

int createDir(char* path)
{
    if (0 != access(path, 0))
    {
        return mkdir(path);
    }
    else
    {
        return 1;
    }
}

int dropDir(char* path)
{
    if (0 == access(path, 0))
    {
        return rmdir(path);
    }
    else
    {
        return 1;
    }
}

void cascadeDropDir(char* path)
{
    char *tPath = (char*)malloc(sizeof(char) * (strlen(path) + 3));
    strcpy(tPath, path);
    tPath[strlen(path)] = '\\';
    tPath[strlen(path) + 1] = '*';
    tPath[strlen(path) + 2] = '\0';

    //参考 https://baijiahao.baidu.com/s?id=1634104829954130656&wfr=spider&for=pc
    struct _finddata_t data;

    long handle = _findfirst(tPath , &data);//-1表示没有文件
    int ret = handle;
    while(ret >= 0)
    {
        if (data.attrib != _A_SUBDIR)
        {
            char t[1000];
            strcpy(t, path);
            strcat(t, "\\");
            strcat(t, data.name);

            remove(t);
        }

        ret = _findnext(handle, &data);
    }
    _findclose(handle);

    dropDir(path);

    free(tPath);

}

int listDir(char *path, char dirList[][20])
{
    char *tPath = (char*)malloc(sizeof(char) * (strlen(path) + 3));
    strcpy(tPath, path);
    tPath[strlen(path)] = '\\';
    tPath[strlen(path) + 1] = '*';
    tPath[strlen(path) + 2] = '\0';

    //参考 https://baijiahao.baidu.com/s?id=1634104829954130656&wfr=spider&for=pc
    struct _finddata_t data;
    int cnt = 0;

    long handle = _findfirst(tPath , &data);//-1表示没有文件
    int ret = handle;
    while(ret >= 0)
    {
        if (data.attrib == _A_SUBDIR && data.name[0] != '.')
            strcpy(dirList[cnt++], data.name);

        ret = _findnext(handle, &data);
    }
    _findclose(handle);

    free(tPath);

    return cnt;
}

int createFile(char *path)
{
    return creat(path, 7)>=0?0:-1;
}

int dropFile(char *path)
{
    return remove(path)>=0?0:-1;
}

int listFile(char *path, char fileList[][20])
{
    char *tPath = (char*)malloc(sizeof(char) * (strlen(path) + 3));
    strcpy(tPath, path);
    tPath[strlen(path)] = '\\';
    tPath[strlen(path) + 1] = '*';
    tPath[strlen(path) + 2] = '\0';

    //参考 https://baijiahao.baidu.com/s?id=1634104829954130656&wfr=spider&for=pc
    struct _finddata_t data;
    int cnt = 0;

    long handle = _findfirst(tPath , &data);//-1表示没有文件
    int ret = handle;
    while(ret >= 0)
    {
        if (data.attrib != _A_SUBDIR)
            strcpy(fileList[cnt++], data.name);

        ret = _findnext(handle, &data);
    }
    _findclose(handle);

    free(tPath);

    return cnt;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MallocLu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值