指定文件夹地址,然后读取文件夹内容。
file.h
#include <string>
#include <vector>
#include <io.h>
/*
* 定义于io.h
* 1、long _findfirst64i32(const char * _Filename,struct _finddata64i32_t * _FindData);
* 查找第一个_Filename的信息,存储到结构体_FindData中
* 查找成功,返回一个用于继续查找的句柄(一个唯一的编号)
* 查找 失败,返回-1
*
* 2、int _findnext64i32(long handle,struct _finddata64i32_t *fileinfo) ;
* 根据句柄handle查找下一个文件,存放在 fileinfo中
* 查找成功,返回0
* 失败返回-1
*
* 3、ing _findclose(long handle);
* 关闭句柄handle
* 成功返回0,
* 失败返回-1
*
* 4、struct _finddata64i32_t
* 用来存储文件的各种信息
* struct _finddata64i32_t
* {
* unsigned attrib;
* __time64_t time_create; // -1 for FAT file systems
* __time64_t time_access; // -1 for FAT file systems
* __time64_t time_write;
* _fsize_t size;
* char name[260];
* };
* unsigned attrib :4个字节,存储文件的属性
* _A_ARCH (存档) 0x20
* _A_SUBDIR(文件夹)0x10
* _A_SYSTEM(系统)0x04
* _A_HIDDEN(隐藏)0x02
* _A_RDONLY(只读)0x01
* _A_NORMAL(正常)0X00
* 以上均是宏,unsigned int,各属性叠加时进行或运算,如_A_HIDDEN|_A_RDONLY
*
*__time64_t time_create: 文件创建的时间
*__time64_t time_access: 文件最后一次访问的时间
*__time64_t time_write: 文件最后以此修改的时间
*_fsize_t size: 文件的大小,字节为单位
*char name[260]: 文件名
*/
enum TKFileInfo
{
FILE_EMPTY = -1, // 目录为空
FILE_SUCCESS, // 查找成功
};
struct fileInfoNode
{
struct _finddata64i32_t fileInfo;
std::string fileName;
struct fileInfoNode* left;
};
typedef std::vector<std::string> VEC_PATH;
class CTKFile
{
public:
CTKFile();
~CTKFile();
public:
int SearchAllFile(std::string filePath,
int layer,
VEC_PATH& vecPath);
private:
int SaveToLink(struct fileInfoNode*& head,
const std::string& fileName,
const struct _finddata64i32_t& fileInfo);
void SaveLinkToFile(struct fileInfoNode* head,
std::vector<std::string>& vecPath,
int counter);
};
file.cpp
#include "TKFile.h"
CTKFile::CTKFile()
{
}
CTKFile::~CTKFile()
{
}
int CTKFile::SaveToLink(struct fileInfoNode*& head,
const std::string& fileName,
const struct _finddata64i32_t& fileInfo)
{
fileInfoNode* p;
p = new fileInfoNode;
p->fileInfo = fileInfo;
p->fileName = fileName;
p->left = head;
head = p;
return 0;
}
void CTKFile::SaveLinkToFile(struct fileInfoNode* head,
std::vector<std::string>& vecPath,
int counter)
{
vecPath.clear();
if (vecPath.capacity() < counter)
{
vecPath.reserve(counter);
}
while (head != NULL)
{
vecPath.push_back(head->fileName);
head = head->left;
}
}
int CTKFile::SearchAllFile(std::string filePath,
int layer,
VEC_PATH& vecPath)
{
struct _finddata64i32_t fileInfo; // 保存文件信息的结构体
static fileInfoNode* head = NULL;
static int counter = 0; // 记录文件数目
long handle; // 句柄
int done; // 查找nextfile是否成功
std::string fileName = filePath + "/*.*"; // 要搜索的文件名
// 查找第一个文件
handle = _findfirst64i32(fileName.c_str(), &fileInfo);
if (handle == -1)
{
return -1;
}
do
{
// 如果是文件夹"."或者"..",则进行判断下一个文件
if ((strcmp(fileInfo.name, ".") == 0) | (strcmp(fileInfo.name, "..") == 0))
{
continue;
}
// 如果是文件夹,则进入下一层文件夹搜索
if ((fileInfo.attrib & _A_SUBDIR) == _A_SUBDIR)
{
std::string filePathSub = filePath + "/" + fileInfo.name;
SearchAllFile(filePathSub, ++layer, vecPath);
--layer;
}
else
{
++counter;
std::string fileNameTure = filePath + "/" + fileInfo.name;
SaveToLink(head, fileNameTure, fileInfo);
}
} while (!(done = _findnext64i32(handle, &fileInfo)));
_findclose(handle);
if (layer == 0)
{
SaveLinkToFile(head, vecPath, counter);
}
return 0;
}