赞
踩
本文记录了使用c++来实现遍历文件的一些实现方法,并在Windows和Linux进行了验证。
示例代码:
#include <iostream> #include <io.h> using namespace std; int PrintFileList(const char *path) { if (!path) return 0; int len = strlen(path) + 3; char *buffer = new char[len]; sprintf(buffer, "%s\\*", path); _finddata_t info; intptr_t handle = _findfirst(buffer, &info);//返回-1表示失败 delete[] buffer; if (handle == -1) { return 0; } int cnt = 0; do { //过滤掉2个特殊的文件 //. 表示当前目录 //..表示上一级目录 if (strcmp(info.name, ".") == 0 || strcmp(info.name, "..") == 0) { continue; } if (info.attrib & _A_SUBDIR) { //如果是文件夹,进行递归遍历 len = strlen(path) + strlen(info.name) + 2; buffer = new char[len]; sprintf(buffer, "%s\\%s", path, info.name); cnt += PrintFileList(buffer); delete[] buffer; } else { //这里只打印出文件的绝对路径 cout << path << "\\" << info.name << endl; ++cnt; } } while (_findnext(handle, &info) == 0); _findclose(handle); return cnt; } int main() { int n = PrintFileList("."); cout << "文件个数:" << n << endl; return 0; }
示例代码:
#include <iostream> #include <Windows.h> using namespace std; int PrintFileList(const TCHAR *path) { if (!path) return 0; int len = lstrlen(path) + lstrlen(TEXT("\\*")) + 1; TCHAR *buffer = new TCHAR[len]; lstrcpy(buffer, path); lstrcat(buffer, TEXT("\\*")); WIN32_FIND_DATA info; HANDLE handle = FindFirstFile(buffer, &info); delete[] buffer; if (handle == INVALID_HANDLE_VALUE) { return 0; } int cnt = 0; do { if (lstrcmp(info.cFileName, TEXT(".")) == 0 || lstrcmp(info.cFileName, TEXT("..")) == 0) { continue; } if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { len = lstrlen(path) + lstrlen(info.cFileName) + lstrlen(TEXT("\\")) + 1; buffer = new TCHAR[len]; lstrcpy(buffer, path); lstrcat(buffer, TEXT("\\")); lstrcat(buffer, info.cFileName); cnt += PrintFileList(buffer); delete[] buffer; } else { #ifdef _UNICODE wcout << path << "\\" << info.cFileName << endl; #else cout << path << "\\" << info.cFileName << endl; #endif ++cnt; } } while (FindNextFile(handle, &info)); FindClose(handle); return cnt; } int main() { int n = PrintFileList(TEXT(".")); cout << "文件个数:" << n << endl; return 0; }
示例代码:
#include <iostream> #include <string.h> #include <dirent.h> using namespace std; int PrintFileList(const char *path) { DIR *dir = opendir(path); if (!dir) return 0; int cnt = 0; dirent *dirent; while ((dirent = readdir(dir))) { if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0) { continue; } /* DT_FIFO = 1, 管道 DT_CHR = 2, 字符设备 DT_DIR = 4, 文件夹 DT_BLK = 6, 块设备 DT_REG = 8, 文件 DT_LNK = 10, 链接 DT_SOCK = 12, 本地套接字 DT_WHT = 14 */ if (dirent->d_type & DT_DIR) { int len = strlen(path) + strlen(dirent->d_name) + 2; char *buffer = new char[len]; sprintf(buffer, "%s/%s", path, dirent->d_name); cnt += PrintFileList(buffer); delete [] buffer; } else { cout << path << "/" << dirent->d_name << endl; ++cnt; } } closedir(dir); return cnt; } int main() { int n = PrintFileList("."); cout << "文件个数:" << n << endl; return 0; }
该方法使用标准库,理论上和平台无关,但是需要支持C++17的编译器才能编译通过。
示例代码:
#include <iostream> #include <filesystem> using namespace std; namespace fs = std::filesystem; int PrintFileList(const fs::path& path) { int n = 0; fs::directory_iterator dir(path); for (const auto& it : dir) { if (it.is_directory()) { n += PrintFileList(it.path()); } else { cout << it.path().string() << endl; ++n; } } return n; } int main() { int n = PrintFileList("."); cout << "文件个数:" << n << endl; return 0; }
- g++命令,添加选项参数-std=c++17
- vs工程配置, 匹配属性=>属性=>常规=>C++语言标准=>ISO C++17标准(/std:c++17)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。