c++ - 如何避免文件扩展名超过三个字符的 CFileFind 误报?

标签 c++ visual-c++ mfc cfilefind

当我尝试调用 CFileFind.FindFile(_T("D://Folder//*.txt")) 时,当唯一的文件是“foobar.txta”时,该方法返回 true .

但是,我不希望文件 foobar.txta 包含在查找结果中。我该怎么做呢?可能通过使用其他方法或其他方法?我是 C++ 的新手 :))

注意:如果扩展名小于 3,则此问题似乎不会发生,例如,如果过滤器为“*.tx”且文件为“foobar.txt”,该方法仍(正确)返回 false。 此外,该问题似乎已报告 here但似乎我需要付费才能看到解决方案

最佳答案

这似乎是 CFindFile(或 Win32 API FindFirstFile)中的错误。但是您可以稍后通过调用 CFindFile::GetFileName 来确定真正的扩展名:

CFindFile finder;
BOOL bWorking = finder.FindFile(L"*.txt");

while (bWorking)
{
    bWorking = finder.FindNextFile();

    if (finder.GetFileName().Right(finder.GetFileName().ReverseFind(L'.')) != L".txt")
        // the file extension is not .txt
}

关于c++ - 如何避免文件扩展名超过三个字符的 CFileFind 误报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12578065/

相关文章:

c++ - MFC C++ 奇怪的问题

c++ - 从 C++ 列表库中打印出列表的内容

C++ 从 linux 到 windows : 'does not evaluate to a constant'

c++ - 使用 cmake 将 obj 文件添加到链接器输入

c++ - 子对话框 - SetWindowTextA 或 SendMessageA 使程序崩溃 - MFC

c++ - 为什么许多 GUI CObjects ie(CButton) 必须放在堆上而不是堆栈上?

c++ - Graphics.h c++ 未定义对各种函数的引用,如 line()、initgraph()

c++ - GMock : How to return mock class variable as the return value

C++ 温度转换

c++ - 从子类 : gcc vs msvc 访问 protected 成员