gpt4 book ai didi

c++ - WIN32_FIND_DATA 在目录中列出文件时返回 "."和 ".."

转载 作者:可可西里 更新时间:2023-11-01 10:03:50 26 4
gpt4 key购买 nike

我正在编写一个应该可以在 Windows 和 Linux 上运行的虚拟文件系统。这是为了一项任务,所以不允许像 Boost 这样的外部事物。对于 Windows 版本,我正在尝试编写一个将所有文件挂载到给定目录中的函数。这是所说的功能:

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 中创建了一个 FileSystem 对象,并在 D: 驱动器上的 TestFolder 路径上调用 MountDirectory:

"D:\TestFolder\*.*"

到目前为止,这段代码有效,但在我的测试文件夹的输出中,它总是在其余文件之前先打印此代码:

Succesfully mounted the file: .

Succesfully mounted the file: ..

为什么这些"file"会被 WIN32_FIND_DATA 获取,我该如何阻止它?

最佳答案

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

关于c++ - WIN32_FIND_DATA 在目录中列出文件时返回 "."和 "..",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41408744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com