如何使用C ++在Windows中列出子目录?

问题描述

| 如何使用C ++在Windows中列出子目录?使用可以跨平台运行的代码更好。     

解决方法

http://msdn.microsoft.com/zh-CN/library/aa364418(v=vs.85).aspx 和 http://msdn.microsoft.com/zh-CN/library/aa365200(v=vs.85).aspx 在Windows Vista / 7或xp:P之间的跨平台     ,这是我针对该问题的解决方案,尽管它只是Windows解决方案。我想使用跨平台解决方案,但不使用Boost。
#include <Windows.h>
#include <vector>
#include <string>

/// Gets a list of subdirectories under a specified path
/// @param[out] output Empty vector to be filled with result
/// @param[in]  path   Input path,may be a relative path from working dir
void getSubdirs(std::vector<std::string>& output,const std::string& path)
{
    WIN32_FIND_DATA findfiledata;
    HANDLE hFind = INVALID_HANDLE_VALUE;

    char fullpath[MAX_PATH];
    GetFullPathName(path.c_str(),MAX_PATH,fullpath,0);
    std::string fp(fullpath);

    hFind = FindFirstFile((LPCSTR)(fp + \"\\\\*\").c_str(),&findfiledata);
    if (hFind != INVALID_HANDLE_VALUE)
    {
        do 
        {
            if ((findfiledata.dwFileAttributes | FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY
                && (findfiledata.cFileName[0] != \'.\'))
            {
                output.push_back(findfiledata.cFileName);
            }
        } 
        while (FindNextFile(hFind,&findfiledata) != 0);
    }
}

/// Gets a list of subdirectory and their subdirs under a specified path
/// @param[out] output Empty vector to be filled with result
/// @param[in]  path   Input path,may be a relative path from working dir
/// @param[in]  prependStr String to be pre-appended before each result
///                        for top level path,this should be an empty string
void getSubdirsRecursive(std::vector<std::string>& output,const std::string& path,const std::string& prependStr)
{
    std::vector<std::string> firstLvl;
    getSubdirs(firstLvl,path);
    for (std::vector<std::string>::iterator i = firstLvl.begin(); 
         i != firstLvl.end(); ++i)
    {
        output.push_back(prependStr + *i);
        getSubdirsRecursive(output,path + std::string(\"\\\\\") + *i + std::string(\"\\\\\"),prependStr + *i + std::string(\"\\\\\"));
    }
}
    ,这是一个比较好的解决方案,应该可以跨平台使用。您将不得不在代码部分更改您希望它执行某些操作的位置,否则它应该可以很好地工作。
#include <cstring>
#include <io.h>
#include <iostream>
#include <stdio.h>

using namespace std;

void list(char* dir)
{
    char originalDirectory[_MAX_PATH];

    // Get the current directory so we can return to it
    _getcwd(originalDirectory,_MAX_PATH);

    _chdir(dir);  // Change to the working directory
    _finddata_t fileinfo;

    // This will grab the first file in the directory
    // \"*\" can be changed if you only want to look for specific files
    intptr_t handle = _findfirst(\"*\",&fileinfo);

    if(handle == -1)  // No files or directories found
    {
        perror(\"Error searching for file\");
        exit(1);
    }

    do
    {
        if(strcmp(fileinfo.name,\".\") == 0 || strcmp(fileinfo.name,\"..\") == 0)
            continue;
        if(fileinfo.attrib & _A_SUBDIR) // Use bitmask to see if this is a directory
            cout << \"This is a directory.\" << endl;
        else
            cout << \"This is a file.\" << endl;
    } while(_findnext(handle,&fileinfo) == 0);

    _findclose(handle); // Close the stream

    _chdir(originalDirectory);
}

int main()
{
    list(\"C:\\\\\");
    return 0;
}
这是非常简洁的代码,它将列出目录中的所有子目录和文件。如果要让它列出每个子目录中的所有内容,则可以通过在打印出“这是目录”的行上传入子目录来递归调用该函数。类似于list(fileinfo。名称);应该可以。     ,看一下Boost.Filesystem。它是跨平台且免费的。     ,您可以使用Dirent围兜。更多细节在这里