在目录中读取所有文件名

Reading all file names in a directory

本文关键字:文件名 读取      更新时间:2023-10-16

我正在尝试读取特定目录中存在的所有文件名。我已经在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解决方案,则可以克隆此存储库。