c++ - 如何在 C++ 中递归查找具有 Unicode 名称的文件?

标签 c++ winapi recursion unicode

我从 here 找到并修改了解决方案:

#include <iostream>
#include <windows.h>
#include <vector>
#include <fstream>

using namespace std;

//wofstream out;

void FindFile(const std::wstring &directory)
{
    std::wcout << endl << endl << endl << "FindFile(" << directory << ")" << std::endl;

    std::wstring tmp = directory + L"\\*";
    WIN32_FIND_DATAW file;
    HANDLE search_handle = FindFirstFileW(tmp.c_str(), &file);
    if (search_handle != INVALID_HANDLE_VALUE)
    {
        std::vector<std::wstring> directories;

        do
        {
            std::wcout << std::endl;
            std::wcout << "    [" << file.cFileName << "]" << std::endl;
            tmp = directory + L"\\" + std::wstring(file.cFileName);

            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if ((!lstrcmpW(file.cFileName, L".")) || (!lstrcmpW(file.cFileName, L".."))) {
                    std::wcout << "continuing..." << std::endl;
                }
                else {
                    std::wcout << "saving path to this directory" << std::endl;
                    directories.push_back(tmp);
                }
            } else {
                std::wcout << "save [" << tmp << "] as file" << std::endl;
            }

            //std::wcout << tmp << std::endl;
            //out << tmp << std::endl;
        }
        while (FindNextFileW(search_handle, &file));

        std::wcout << "all items inside current directory was worked out. close it's handle." << std::endl;
        FindClose(search_handle);

        for(std::vector<std::wstring>::iterator iter = directories.begin(), end = directories.end(); iter != end; ++iter) {
            std::wcout << "recursively find in next directory: [" << *iter << "]" << std::endl;
            FindFile(*iter);
        }
    } else {
        std::wcout << "invalid handle value" << std::endl;
    }
}

int main()
{
    //out.open("C:\\temp\\found.txt");

    FindFile(L"C:\\test");

    //out.close();

    cout << "The end" << endl;
    string str;
    cin >> str;

    return 0;
}

但此代码不适用于具有西里尔字母名称的文件夹或文件(但我使用所有类型和函数的 Unicode 版本!)

更新:应用程序刚刚完成,没有任何异常,就像执行了所有命令一样。

Update-2(打印屏幕): print-screen

谁有同样的问题?感谢您的帮助。

已解决 非常感谢@zett42! 经过一些重构后,工作代码如下所示:

#include <iostream>
#include <windows.h>
#include <vector>
#include <fstream>
#include <io.h>
#include <fcntl.h>

using namespace std;

vector<wstring> FindFiles(const std::wstring &directory) {
    vector<wstring> files;
    std::vector<std::wstring> directories;

    std::wstring fullPath = directory + L"\\*";
    WIN32_FIND_DATAW file;
    HANDLE search_handle = FindFirstFileW(fullPath.c_str(), &file);

    if (search_handle == INVALID_HANDLE_VALUE)
        return files;

    do
    {
        fullPath = directory + L"\\" + std::wstring(file.cFileName);

        if (!(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            files.push_back(fullPath);
        else {
            if ((lstrcmpW(file.cFileName, L".")) && (lstrcmpW(file.cFileName, L"..")))
                directories.push_back(fullPath);
        }
    }
    while (FindNextFileW(search_handle, &file));

    FindClose(search_handle);

    for(std::vector<std::wstring>::iterator iter = directories.begin(), end = directories.end(); iter != end; ++iter) {
        vector<wstring> newFiles = FindFiles(*iter);
        files.insert(files.begin(), newFiles.begin(), newFiles.end());
    }

    return files;
}

int main()
{
    _setmode( _fileno(stdout), _O_U16TEXT );

    vector<wstring> files = FindFiles(L"E:\\test");

    wcout << L"All found files: " << endl;
    for (int i = 0; i < files.size(); ++i)
        wcout << files[i] << endl;

    cout << "The end" << endl;
    string str;
    cin >> str;

    return 0;
}

最佳答案

在 Windows 上,默认情况下,即使您使用 std::wcout,Unicode 输出到控制台也不起作用。

要使其正常工作,请在程序开头插入以下行:

_setmode( _fileno(stdout), _O_U16TEXT );

_setmode_fileno是微软特有的功能。

您可能还需要更改控制台字体。我正在使用 Lucida Console,它可以很好地处理西里尔字母。

完整示例:

#include <iostream>
#include <io.h>      // _setmode()
#include <fcntl.h>   // _O_U16TEXT

int main()
{
    // Windows needs a little non-standard magic for Unicode console output.
    _setmode( _fileno(stdout), _O_U16TEXT );

    std::wcout << L"по русски\n";
}

由于 Unicode 字符串文字,示例应保存为 UTF-8 编码文件,但这与您的情况无关,因为您没有 Unicode 字符串文字。

我已经在Win10上的MSVC2015和MSVC2017下成功测试了这段代码。

关于c++ - 如何在 C++ 中递归查找具有 Unicode 名称的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43329565/

相关文章:

java - 两种不同的递归方式

php - 递归功能 block 的最后一条语句执行了多少次?

c++ - wstring 的 isupper 和 islower

c++ - 如何创建 union 类结构?

c - WM_TIMER 在特定条件下丢失

python - 我可以递增一个变量,将其初始化为 0 作为递归函数中的参数,最后简单地将这个数字附加到数组中吗?

C++ ofstream换行符

c++ - 找到给定数量的素数

C++ 定时器问题

Python Win32 - 与 DriveInfo.IsReady 等效的函数