# include<iostream>
# include<io.h>
# include<string>
# include<direct.h>
# include<vector>
using namespace std;
int main()
{
//获取当前路径:
char path1[1024];
getcwd(path1, 1024);
string path = path1;
printf("当前路径为:%s\n", path.c_str());
//搜索文件
string search_file = path + "\\" + "*.bmp";//一定写成通配符,搜索所有文件写成"*"
struct _finddata_t document; //建立查找文件结构体;
intptr_t index = 0;//用于查找的句柄;在32位系统中不能使用long类型,在64位中可以,intptr_t则通用
//下面的_findfirst 查找成功返回任意值,失败返回-1; \
_findnext 查找成功返回 0, 失败返回-1; \
_findclose 成功返回 0, 失败返回-1;
vector<string> file_name;
index = _findfirst(search_file.c_str(), &document);
while (!_findnext(index, &document))
{
file_name.push_back(document.name);
}
int count = 1;
for (auto &ele : file_name)
{
printf("%s:\t", (("第" + to_string(count) + "个文件").c_str()));
cout << ele << endl;
count++;
}
_findclose(handle);
getchar();
return 0;
}
/*
注:_finddata_t结构中还含有如下参数:
struct _finddata_t
{
unsigned attrib; 文件属性
time_t time_create; 创建时间
time_t time_access; 访问时间(最后)
time_t time_write; 写入时间(最后)
_fsize_t size; 文件大小(字节)
char name[_MAX_FNAME];(文件名长度)
};
*/