如何使用C++;? 我如何用C++来列出Windows中的子目录?使用跨平台运行的代码更好。
及如何使用C++;? 我如何用C++来列出Windows中的子目录?使用跨平台运行的代码更好。,c++,windows,subdirectory,C++,Windows,Subdirectory,及 它在WindowsVista/7或xp之间的跨平台:请看。它是跨平台和免费的。这是我解决这个问题的方法,但它是一个仅限Windows的方法。我想使用跨平台解决方案,但不想使用boost #include <Windows.h> #include <vector> #include <string> /// Gets a list of subdirectories under a specified path /// @param[out] outpu
它在WindowsVista/7或xp之间的跨平台:请看。它是跨平台和免费的。这是我解决这个问题的方法,但它是一个仅限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("\\"));
}
}
#包括
#包括
#包括
///获取指定路径下的子目录列表
///@param[out]输出要用结果填充的空向量
///@param[in]路径输入路径,可能是工作目录的相对路径
void getSubdirs(std::vector&output,const std::string&path)
{
WIN32_FIND_DATA findfiledata;
句柄hFind=无效的句柄值;
字符完整路径[最大路径];
GetFullPathName(path.c_str(),MAX_path,fullpath,0);
std::字符串fp(完整路径);
hFind=FindFirstFile((LPCSTR)(fp+“\\*”).c_str(),&findfiledata);
if(hFind!=无效的句柄值)
{
做
{
if((findfiledata.dwFileAttributes | FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY
&&(findfiledata.cFileName[0]!='。)
{
output.push_back(findfiledata.cFileName);
}
}
while(FindNextFile(hFind,&findfiledata)!=0);
}
}
///获取指定路径下的子目录及其子目录的列表
///@param[out]输出要用结果填充的空向量
///@param[in]路径输入路径,可能是工作目录的相对路径
///@param[in]prependStr字符串要在每个结果之前预先追加
///对于顶级路径,这应该是一个空字符串
void GetSubdersRecursive(标准::向量和输出),
常量std::字符串和路径,
常量std::string和prependStr)
{
std::vector firstLvl;
获取子目录(第一层,路径);
for(std::vector::iterator i=firstLvl.begin();
i!=firstLvl.end();+i)
{
输出。推回(prependStr+*i);
GetSubdersRecursive(输出,
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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
无效列表(字符*目录)
{
字符源目录[_MAX_PATH];
//获取当前目录,以便我们可以返回到它
_getcwd(原始目录,最大路径);
_chdir(dir);//更改为工作目录
_finddata_t fileinfo;
//这将获取目录中的第一个文件
//如果只想查找特定文件,则可以更改“*”
intptr\u t handle=\u findfirst(“*”,&fileinfo);
if(handle==-1)//未找到任何文件或目录
{
perror(“文件搜索错误”);
出口(1);
}
做
{
如果(strcmp(fileinfo.name,“.”)==0 | | strcmp(fileinfo.name,“…”)==0)
继续;
if(fileinfo.attrib&_A_SUBDIR)//使用位掩码查看这是否是一个目录
cout您可以使用dirent bib。更多详细信息是:首先,您搜索,然后尝试一些东西,然后将代码张贴在卡住的地方。跨平台?可能与@Etienne重复:为什么要链接到一个将近两年的版本的文档?Boost。从那时起,文件系统的API发生了重大变化(v3与v2),所以值得链接到它。@avee:你说你想要一个Windows解决方案,最好是跨平台的,并且遵守POSIX标准?下定决心…;-](尽管如此,还是选择一个。)隐藏的技巧是GetFileAttributes将指示一个文件是目录还是常规文件。而且,这个解决方案肯定能在XP上工作。(也可能是Windows95)@selbie您还可以检查上述函数的WIN32\u FIND\u DATA.dwFileAttributes
部分结果,查看FILE\u ATTRIBUTE\u DIRECTORY
值,以检查结果是否为目录。此外,您还应该检查
和。
因为它们也是作为结果返回的。选择此作为答案,因为这是我想要的在我的解决方案中实际使用。你介意将链接文章的相关部分包含在你的答案中吗?链接可能会改变,使你的答案过时,对未来的读者毫无用处。请参阅.thanx以获取答案。非常有用。此算法不会检测带有自定义图标的文件夹。这是一种错误的方式,alg,抱歉:)此算法似乎正确地执行递归,但是getSubdirs无法可靠地检测文件是否为目录类型。以下if条件不正确:if(findfiledata.dwFileAttributes | file _ATTRIBUTE | directory)==FILE_ATTRIBUTE_DIRECTORY)调整有问题的if条件以使用以下条件,使算法对我来说工作可靠:if(findfiledata.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)请参阅以了解更多详细信息