本文实例讲述了C++遍历文件夹下文件的方法。分享给大家供大家参考。具体如下:
#include <@[email protected]>#include <stdio.h>#include <string.h>#define LEN 1024// 深度优先递归遍历目录中所有的文件BOol DirectoryList(LPCSTR Path){ WIN32_FIND_DATA FindData; HANDLE hError; int fileCount = 0; char filePathname[LEN]; // 构造路径 char FullPathname[LEN]; strcpy(filePathname,Path); strcat(filePathname,"\*.*"); hError = FindFirstfile(filePathname,&FindData); if (hError == INVALID_HANDLE_VALUE) { printf("搜索失败!"); return 0; } while(::FindNextfile(hError,&FindData)) { // 过虑.和.. if (strcmp(FindData.cfilename,".") == 0 || strcmp(FindData.cfilename,"..") == 0 ) { continue; } // 构造完整路径 wsprintf(FullPathname,"%s\%s",Path,FindData.cfilename); fileCount++; // 输出本级的文件 printf("\n%d %s ",fileCount,FullPathname); if (FindData.DWfileAttributes & file_ATTRIBUTE_DIRECTORY) { printf("<Dir>"); DirectoryList(FullPathname); } } return 0;}voID main(){ DirectoryList("D:eclipse-J2EE");}
希望本文所述对大家的C++程序设计有所帮助。
总结以上是内存溢出为你收集整理的C++遍历文件夹下文件的方法全部内容,希望文章能够帮你解决C++遍历文件夹下文件的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)