您的位置:首页 > 编程语言 > C语言/C++

windows C++ 遍历目录,获取文件名和文件路径

2017-08-04 18:06 531 查看
void  GetFiles(const char* srcPath, vector<string> &filesPath,  vector<string>& filesName)
{
WIN32_FIND_DATAA ffd;
memset(&ffd, 0, sizeof(ffd));
string path = srcPath;
path.append("\\*");
HANDLE hFind = FindFirstFileA(path.c_str(),
&ffd);
do {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (!strcmp(ffd.cFileName,".") || !strcmp(ffd.cFileName,
".."))
continue;
string newPath;
newPath.append(srcPath).append("\\").append(ffd.cFileName);
getFileName(files, newPath.c_str(),
imageName);
}
else
{
char filePath[MAX_LEN_256] = { 0 };
sprintf_s(filePath, "%s/%s", srcPath,
ffd.cFileName);
imageName.push_back(ffd.cFileName);
files.push_back(filePath);
}
} while (FindNextFileA(hFind, &ffd)
!= 0);
FindClose(hFind);
}
void  GetFiles(const char* srcPath, vector<string> &filesPath,  vector<string>& filesName)
{
WIN32_FIND_DATAA ffd;
memset(&ffd, 0, sizeof(ffd));
string path = srcPath;
path.append("\\*");
HANDLE hFind = FindFirstFileA(path.c_str(),
&ffd);
do {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (!strcmp(ffd.cFileName,".") || !strcmp(ffd.cFileName,
".."))
continue;
string newPath;
newPath.append(srcPath).append("\\").append(ffd.cFileName);
getFileName(files, newPath.c_str(),
imageName);
}
else
{
char filePath[MAX_LEN_256] = { 0 };
sprintf_s(filePath, "%s/%s", srcPath,
ffd.cFileName);
imageName.push_back(ffd.cFileName);
files.push_back(filePath);
}
} while (FindNextFileA(hFind, &ffd)
!= 0);
FindClose(hFind);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐