在目录中读取所有文件名
Reading all file names in a directory
我正在尝试读取特定目录中存在的所有文件名。我已经在C 中制作了一个程序,但这仅在此目录中直接打印文件。我想要子目录中也存在的所有文件。
我已经在C 中编写了一个程序,该程序在目录中打印文件名,但我也希望子目录中的所有文件名。
#include <stdio.h>
#include <windows.h>
#include <bits/stdc++.h>
#include <dirent.h>
using namespace std;
#include <sys/types.h>
#include <sys/stat.h>
#ifndef WIN32
#include <unistd.h>
#endif
#ifdef WIN32
#define stat _stat
#endif
int main ()
{
DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\test")) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
printf ("%sn", ent->d_name);
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}
}
实际结果:1。打印的文件名直接在目录中并打印子目录名称。
预期:我想要而不是打印子目录名称该程序应在该子目录中打印文件名。
如果可用C 17,请使用recursive_directory_iterator
。如果没有,则可以使用dirent.h
函数。例如,考虑以下通用traverseFiles
函数,该功能将每个文件传递到处理检测到的文件的函数:
#include <iostream>
#include <dirent.h>
#include <string>
void traverseFiles(const std::string &path, std::function<void(const std::string &)> cb) {
if (auto dir = opendir(path.c_str())) {
while (auto f = readdir(dir)) {
if (f->d_name[0] == '.') continue;
if (f->d_type == DT_DIR)
traverseFiles(path + f->d_name + "/", cb);
if (f->d_type == DT_REG)
cb(path + f->d_name);
}
closedir(dir);
}
}
void fileDetected(const std::string &f) {
std::cout << "file:" << f << std::endl;
}
int main() {
traverseFiles("c:/somestartdir", &fileDetected);
}
使用C 17 recursive_directory_iterator :
#include <filesystem>
void ls_recursive(const std::filesystem::path& path) {
for(const auto& p: std::filesystem::recursive_directory_iterator(path)) {
std::cout << p.path() << 'n';
}
}
如果您使用的是Windows OS,则可以在代码下运行以在提供的目录中获取文件列表。
#include <windows.h>
#include <TCHAR.h>
#include <stdio.h>
void Enum(TCHAR root[0xFF])
{
HANDLE hFind;
WIN32_FIND_DATA wfd;
TCHAR GeneralPath[0xFF];
TCHAR AgainFolder[0xFF];
TCHAR FileFullPath[0xFF];
_stprintf(GeneralPath, _T("%s\*.*"), root);
hFind = FindFirstFile(GeneralPath, &wfd);
if(INVALID_HANDLE_VALUE==hFind)
return;
do
{
if( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) //Checking Founded File Attribute is it File or Folder/Directory
{
if( !_tcscmp(wfd.cFileName, _T(".")) || !_tcscmp(wfd.cFileName, _T("..")) ) //if Founded directory is same(.) or parent(..) then ignore them
continue;
_stprintf(AgainFolder, _T("%s\%s"), root, wfd.cFileName);
Enum(AgainFolder); //Recursion because folder is inside another folder
}
else
{
_stprintf(FileFullPath, _T("%s\%s"), root, wfd.cFileName); // "Folder\fileName.extension"
_tprintf(_T("%sn"),FileFullPath);
}
}while(FindNextFile(hFind, &wfd));
CloseHandle(hFind);
hFind=INVALID_HANDLE_VALUE;
}
int main()
{
TCHAR Folder[0xFF]=_T("c:\windows");
Enum(Folder);
return 0;
}
如果要访问完整的Visual Studio解决方案,则可以克隆此存储库。
相关文章:
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- C++ 从标准输入读取文件名
- 输入文件名并读取文本
- 使用 C++ 读取由 txt 文件名引用的特定列的值
- 如何在C 中读取具有前缀和时间戳为文件名的文件
- 在目录中读取所有文件名
- 如何从文本文件中读取文件名列表并用C++打开它们
- 使用 c++ 读取音乐文件名
- 我在哪里放置程序要读取的文件名
- 如果从文件中读取,则无法按文件名运行进程
- 如何读取目录中上次修改的文件的文件名
- 从命令行读取文件名并在visualstudio中打开
- 从文件名读取日期
- 从命令行读取文件名,并在我的文件中使用它
- 读取非ascii文件名的文件
- 打开并读取在运行时指定文件名的文件
- 在c++中读取文件名的一部分
- 如何从文件夹中读取文件?文件夹包含不同类型的文件名
- 如何将目录的特定文件名读取到数组中
- 如何使用 unicode 文件名 c++ 读取二进制文件