struct_finddata_t
{
unsignedattrib;//文件属性
time_ttime_create;//文件创建时间
time_ttime_access;//文件上一次访问时间
time_ttime_write;//文件上一次修改时间
_fsize_tsize;//文件字节数
charname[_MAX_FNAME];//文件名
};
文件属性是无符号整数,取值为相应的宏:_A_ARCH(存档),_A_SUBDIR(文件夹),_A_HIDDEN(隐藏),_A_SYSTEM(系统),_A_NORMAL(正常),_A_RDONLY(只读)。容易看出,通过这个结构体,我们可以得到关于该文件的很多信息。结合以下函数,我们可以将文件信息存储到这个结构体中:
//按FileName命名规则匹配当前目录第一个文件
_findfirst(_In_constchar*FileName,_Out_struct_finddata64i32_t *_FindData);
//按FileName命名规则匹配当前目录下一个文件
_findnext(_In_ intptr_t_FindHandle,_Out_struct_finddata64i32_t *_FindData);
//关闭_findfirst返回的文件句柄
_findclose(_In_ intptr_t_FindHandle);
//按FileName命名规则匹配当前目录第一个文件
_findfirst(_In_constchar*FileName,_Out_struct_finddata64i32_t *_FindData);
//按FileName命名规则匹配当前目录下一个文件
_findnext(_In_ intptr_t_FindHandle,_Out_struct_finddata64i32_t *_FindData);
//关闭_findfirst返回的文件句柄
_findclose(_In_ intptr_t_FindHandle);
_findfirst 函数返回的是匹配到文件的句柄,数据类型为long。遍历过程可以指定文件类型。
实例
#include
#include
#include
usingnamespacestd;
//遍历当前目录下的文件夹和文件,默认是按字母顺序遍历
boolTraverseFiles(stringpath,int&file_num)
{
_finddata_tfile_info;
stringcurrent_path=path+"/*.*";//可以定义后面的后缀为*.exe,*.txt等来查找特定后缀的文件,*.*是通配符,匹配所有类型,路径连接符最好是左斜杠/,可跨平台
//打开文件查找句柄
inthandle=_findfirst(current_path.c_str(),&file_info);
//返回值为-1则查找失败
if(-1==handle)
returnfalse;
do
{
//判断是否子目录
stringattribute;
if(file_info.attrib==_A_SUBDIR)//是目录
attribute="dir";
else
attribute="file";
//输出文件信息并计数,文件名(带后缀)、文件最后修改时间、文件字节数(文件夹显示0)、文件是否目录
cout<
file_num++;
}while(!_findnext(handle,&file_info));//返回0则遍历完
//关闭文件句柄
_findclose(handle);
returntrue;
}
//深度优先递归遍历当前目录下文件夹和文件及子文件夹和文件
voidDfsFolder(stringpath,intlayer)
{
_finddata_tfile_info;
stringcurrent_path=path+"/*.*";//也可以用/*来匹配所有
inthandle=_findfirst(current_path.c_str(),&file_info);
//返回值为-1则查找失败
if(-1==handle)
{
cout<
return;
}
do
{
//判断是否子目录
if(file_info.attrib==_A_SUBDIR)
{
//递归遍历子目录
//打印记号反映出深度层次
for(inti=0;i
cout<
cout<
intlayer_tmp=layer;
if(strcmp(file_info.name,"..")!=0&&strcmp(file_info.name,".")!=0)//.是当前目录,..是上层目录,必须排除掉这两种情况
DfsFolder(path+'/'+file_info.name,layer_tmp+1);//再windows下可以用转义分隔符,不推荐
}
else
{
//打印记号反映出深度层次
for(inti=0;i
cout<
cout<
}
}while(!_findnext(handle,&file_info));//返回0则遍历完
//关闭文件句柄
_findclose(handle);
}
intmain(intargc,char*argv[])
{
//遍历单个目录
intfile_num=0;
if(!TraverseFiles("E:/android-ndk",file_num))//此处路径连接符只能用/,根盘符大小写都行
cout<
cout<
//递归遍历文件夹
DfsFolder("E:/personal_profile/tinyxml",0);
return0;
}
#include
#include
#include
usingnamespacestd;
//遍历当前目录下的文件夹和文件,默认是按字母顺序遍历
boolTraverseFiles(stringpath,int&file_num)
{
_finddata_tfile_info;
stringcurrent_path=path+"/*.*";//可以定义后面的后缀为*.exe,*.txt等来查找特定后缀的文件,*.*是通配符,匹配所有类型,路径连接符最好是左斜杠/,可跨平台
//打开文件查找句柄
inthandle=_findfirst(current_path.c_str(),&file_info);
//返回值为-1则查找失败
if(-1==handle)
returnfalse;
do
{
//判断是否子目录
stringattribute;
if(file_info.attrib==_A_SUBDIR)//是目录
attribute="dir";
else
attribute="file";
//输出文件信息并计数,文件名(带后缀)、文件最后修改时间、文件字节数(文件夹显示0)、文件是否目录
cout<
file_num++;
}while(!_findnext(handle,&file_info));//返回0则遍历完
//关闭文件句柄
_findclose(handle);
returntrue;
}
//深度优先递归遍历当前目录下文件夹和文件及子文件夹和文件
voidDfsFolder(stringpath,intlayer)
{
_finddata_tfile_info;
stringcurrent_path=path+"/*.*";//也可以用/*来匹配所有
inthandle=_findfirst(current_path.c_str(),&file_info);
//返回值为-1则查找失败
if(-1==handle)
{
cout<
return;
}
do
{
//判断是否子目录
if(file_info.attrib==_A_SUBDIR)
{
//递归遍历子目录
//打印记号反映出深度层次
for(inti=0;i
cout<
cout<
intlayer_tmp=layer;
if(strcmp(file_info.name,"..")!=0&&strcmp(file_info.name,".")!=0)//.是当前目录,..是上层目录,必须排除掉这两种情况
DfsFolder(path+'/'+file_info.name,layer_tmp+1);//再windows下可以用转义分隔符,不推荐
}
else
{
//打印记号反映出深度层次
for(inti=0;i
cout<
cout<
}
}while(!_findnext(handle,&file_info));//返回0则遍历完
//关闭文件句柄
_findclose(handle);
}
intmain(intargc,char*argv[])
{
//遍历单个目录
intfile_num=0;
if(!TraverseFiles("E:/android-ndk",file_num))//此处路径连接符只能用/,根盘符大小写都行
cout<
cout<
//递归遍历文件夹
DfsFolder("E:/personal_profile/tinyxml",0);
return0;
}
(ps:貌似注释多了点,有点碍眼T_T)
觉得本文有帮助?请分享给更多人
关注「CPP开发者」
看更多精选C/C++技术文章
↓↓↓