
使用方法:将.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("%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
#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 ; datalong
= handle _findfirst (,tPath & )data;//-1表示没有文件int
= ret ; handlewhile
(0ret >= )if
{
( .data!=attrib ) _A_SUBDIRchar
{
[ t1000];strcpy
(,t) path;strcat
(,t"\" );strcat
(,t. data)name;remove
()t;}
=
ret _findnext (,handle& )data;}
_findclose
()handle;dropDir
()path;free
()tPath;}
int
listDir (char* ,pathchar [ dirList][20])char
{
* =tPath ( char*)malloc(sizeof(char)* ( strlen()path+ 3 ));strcpy
(,tPath) path;[
tPathstrlen()path]= '\' ;[
tPathstrlen()path+ 1 ]= '*' ;[
tPathstrlen()path+ 2 ]= ';' //参考 https://baijiahao.baidu.com/s?id=1634104829954130656&wfr=spider&for=pcstruct
_finddata_t
; int data=
0 cnt ; long=
_findfirst handle ( ,&tPath ) ;data//-1表示没有文件int=
; ret while handle(
0)ret >= if(
{
. ==data&&attrib . _A_SUBDIR [ data0name]!='.' ) strcpy(
[++dirList]cnt,.) data;name=_findnext
ret ( ,&handle) ;data}_findclose
(
);handlefree(
);tPathreturn;
} cntint
createFile
( char*) returnpathcreat
{
( ,7path) 0?>=0:-1;}int
dropFile
( char*) returnpathremove
{
( )0path?>=0:-1;}int
listFile
( char*, charpath[ ] fileList[20])char*
{
= (tPath char *)malloc(sizeof(char)*( strlen ()+path3 ) );strcpy(
,)tPath; path[strlen
tPath()]path='\' ; [strlen
tPath()+path1 ] ='*' ; [strlen
tPath()+path2 ] =';' //参考 https://baijiahao.baidu.com/s?id=1634104829954130656&wfr=spider&for=pc struct_finddata_t
;
int = data0
; cnt long =_findfirst
( handle , &)tPath ; //-1表示没有文件dataint=;
while ret ( handle0
)ifret >= (.
{
!= )datastrcpyattrib ( _A_SUBDIR[
++]fileList,cnt.); data=name_findnext(
ret , &)handle; }data_findclose(
)
;freehandle()
;returntPath;}
cnt
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)