C++ 列出WinApi目录中的文件
在目录中,我只有3个文件(poem.txt、mac.txt、some_file.txt) 为什么在我的输出中有两个名为“.”和“.”的奇怪文件 那是什么C++ 列出WinApi目录中的文件,c++,file,winapi,C++,File,Winapi,在目录中,我只有3个文件(poem.txt、mac.txt、some_file.txt) 为什么在我的输出中有两个名为“.”和“.”的奇怪文件 那是什么 #include <iostream> #include <Windows.h> #include <string> using namespace std; int main() { WIN32_FIND_DATA fileData; HANDLE hFind = FindFirstFil
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
int main()
{
WIN32_FIND_DATA fileData;
HANDLE hFind = FindFirstFile(L"D:\\Univesity\\PROJECTS\\OS_Course\\poem_old\\*", &fileData);
if (hFind != INVALID_HANDLE_VALUE) {
do {
//wcout << fileData.cFileName << endl;
WIN32_FIND_DATA compared_fileData;
HANDLE compared_hFind = FindFirstFile(L"D:\\Univesity\\PROJECTS\\OS_Course\\poem2_new\\*", &compared_fileData);
if (compared_hFind != INVALID_HANDLE_VALUE) {
do {
//wcout << (wstring) fileData.cFileName << endl << (wstring) compared_fileData.cFileName;
if ( (wstring) fileData.cFileName == (wstring) compared_fileData.cFileName) {
FILETIME fileWriteTime = fileData.ftLastWriteTime;
FILETIME compared_fileWriteTime = compared_fileData.ftLastWriteTime;
/*
-1 First file time is earlier than second file time.
0 First file time is equal to second file time.
1 First file time is later than second file time.
*/
int comparationResult = CompareFileTime(&fileWriteTime, &compared_fileWriteTime);
if (comparationResult == 1) {
wcout << "Comparation: " << (wstring)fileData.cFileName << " is later than " << (wstring)compared_fileData.cFileName << endl;
}
else if (comparationResult == -1) {
wcout << "Comparation: " << (wstring)fileData.cFileName << " is earlier than " << (wstring)compared_fileData.cFileName << endl;
}
}
} while (FindNextFile(compared_hFind, &compared_fileData));
}
} while (FindNextFile(hFind, &fileData));
FindClose(hFind);
}
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
WIN32_查找_数据文件数据;
HANDLE hFind=FindFirstFile(L“D:\\Univesity\\PROJECTS\\OS\u Course\\poem\u old\\\*”,&fileData);
if(hFind!=无效的句柄值){
做{
//wcout
和。
是伪目录。语义如下:
- 使用句点作为路径中的目录组件来表示当前目录,例如“\temp.txt”。有关更多信息,请参阅
- 使用两个连续的句点(..)作为路径中的目录组件,以表示当前目录的父目录,例如“.\temp.txt”。有关详细信息,请参阅
在命令提示符下,键入dir
是当前目录,是父目录。旁注:您真的需要WinAPI中的一些特殊功能来完成此操作吗?为什么不使用std::filesystem
(从C++17开始提供)?@TedLyngmo我通常不使用C++,所以我对它知之甚少。谢谢你的建议。@TedLyngmo:std::filesystem::diretory_迭代器
可以处理这种特殊情况。但是,总的来说,它有很多折衷之处,归结起来就是作为Windows和Posix风格文件系统的最小公分母包装器,这让它很难实现执行本机API所做的事情,如通配符的一致应用、处理长文件名和8.3文件名,以及某些类型的高效搜索。@ted:我的观点是,不同平台的文件系统差异太大,类似于“正常操作”根本不存在。我可能已经使用了所有C++,而<代码> STD::文件系统< /C>是我唯一会避免的唯一库。