栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

#include 

#include "FileDirTools.h"

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

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

//    //dropDir使用
//    int drF = dropDir("Database");
//    printf("%dn", drF);

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



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

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

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

    return 0;
}
//FileDirTools.h


#ifndef FILEDIRTOOLS_H_INCLUDED
#define FILEDIRTOOLS_H_INCLUDED

#include 
#include 
#include 
#include 

//在指定路径下创建文件夹
//返回值   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] = '';

    //参考 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] = '';

    //参考 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] = '';

    //参考 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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/832726.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号