- char * filePath = "D:\\sample";
- vector<string> files;
- ////获取该路径下的所有文件
- getFiles(filePath, files );
- char str[30];
- int size = files.size();
- for (int i = 0;i < size;i++)
- {
- <span style="white-space:pre"> </span>cout<<files[i].c_str()<<endl;
- }
- void getFiles( string path, vector<string>& files )
- {
- //文件句柄
- long hFile = 0;
- //文件信息
- struct _finddata_t fileinfo;
- string p;
- if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
- {
- do
- {
- //如果是目录,迭代之
- //如果不是,加入列表
- if((fileinfo.attrib & _A_SUBDIR))
- {
- if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
- getFiles( p.assign(path).append("\\").append(fileinfo.name), files );
- }
- else
- {
- files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
- }
- }while(_findnext(hFile, &fileinfo) == 0);
- _findclose(hFile);
- }
- }
本文介绍了一个使用C++实现的递归遍历指定目录及其子目录下所有文件的方法,并提供了完整的代码示例。该方法利用了_findfirst和_findnext函数来获取文件列表,并判断是否为目录进行递归操作。
1128

被折叠的 条评论
为什么被折叠?



