如何使用 C++ 在 Windows 中列出子目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6133647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I list subdirectories in Windows using C++?
提问by avee
how do I list subdirectories in windows using C++? Using code that would run cross-platorm is better.
如何使用 C++ 列出 Windows 中的子目录?使用可以跨平台运行的代码会更好。
采纳答案by uray
http://msdn.microsoft.com/en-us/library/aa364418(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/aa364418(v=vs.85).aspx
and
和
http://msdn.microsoft.com/en-us/library/aa365200(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/aa365200(v=vs.85).aspx
its crossplatform between windows vista/7 or maybe xp :P
它在 windows vista/7 或 xp 之间的跨平台:P
回答by avee
Here's my solution for the problem, it is a Windows only solution though. I want to use a cross-platform solution, but not using boost.
这是我对这个问题的解决方案,不过它是一个仅限 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("\"));
}
}
回答by legacybass
Here's a relatively good solution that should work cross platform. You'll have to change the section of the code where you want it to do something but otherwise it should work quite well.
这是一个相对较好的解决方案,应该可以跨平台工作。您必须更改您希望它执行某些操作的代码部分,否则它应该可以很好地工作。
#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;
}
This is pretty concise code and will list all sub-directories and files in the directory. If you want to have it list all the contents in each sub-directory, you can recursively call the function by passing in the sub-directory on the line that prints out "This is a directory." Something like list(fileinfo.name); should do the trick.
这是非常简洁的代码,将列出目录中的所有子目录和文件。如果你想让它列出每个子目录中的所有内容,你可以通过在打印出“这是一个目录”的行上传入子目录来递归调用该函数。类似于 list(fileinfo.name); 应该做的伎俩。
回答by Juraj Blaho
Look at Boost.Filesystem. It's cross-platform and free.
看看Boost.Filesystem。它是跨平台和免费的。