列出目录中的文件时,WIN32_FIND_DATA返回“。”和“..”

我正在写一个虚拟文件系统,可以在Windows和Linux上工作。 这是一个任务,所以外部的东西,如升压是不允许的。 对于Windows版本,我试图编写一个函数,将所有文件装载到给定的目录中。 这里是说function:

void FileSystem::MountDirectory(const std::string directory) { WIN32_FIND_DATA search_data; memset(&search_data, 0, sizeof(WIN32_FIND_DATA)); std::wstring wDir = StringToWstring(directory); LPCWSTR dir = wDir.c_str(); HANDLE handle = FindFirstFile(dir, &search_data); if (handle == INVALID_HANDLE_VALUE) { std::cout << "ERROR: Unable to mount files in path: " << directory << std::endl; } else { while (handle != INVALID_HANDLE_VALUE) { std::string fileName = WCHARArrayToString(search_data.cFileName); File file(fileName, directory); m_MountedFiles.push_back(file); std::cout << "Succesfully mounted the file: " << fileName << std::endl; if (FindNextFile(handle, &search_data) == FALSE) { std::cout << "No more files to mount." << std::endl; break; } } } FindClose(handle); } 

我做了一些内联函数帮助从std :: string转换为std :: wstring,反之亦然。 我在我的main.cpp中创build一个FileSystem对象,然后调用path上的MountDirectory到我的D:驱动器上的TestFolder:

“d:\ TestFolder \ *。*”。

到目前为止,这段代码可以正常工作,但是在我的testing文件夹的输出中,它总是先打印这些文件,

成功安装文件:。

成功安装文件:

为什么这些“文件”被WIN32_FIND_DATA拾取,我如何防止它?

这些分别表示当前目录和父目录。 如果您正在枚举所有文件和目录,则无法禁止枚举这些对象。 如果你不想打印它们,让你的代码检测它们,并忽略它们。