C++ windows fstream区分大小写

dk1*_*123 3 c++ fstream

我注意到在Windows上,文件打开不区分大小写.

(例如,fstream("text.txt")无论打开的实际文件名存在的:Text.txt)

我如何才能使这个区分大小写呢?(除非文件名也匹配正确的情况,否则文件无法打开)

Jon*_*ter 6

在Windows下,文件系统API通常不区分大小写,因此唯一的方法是自己检查文件名的大小写.例如,

bool open_stream_ci(const char* pszName, std::fstream& out)
{
    WIN32_FIND_DATAA wfd;
    HANDLE hFind = ::FindFirstFileA(pszName, &wfd);
    if (hFind != INVALID_HANDLE_VALUE)
    {
        ::FindClose(hFind);
        if (!strcmp(wfd.cFileName, ::PathFindFileNameA(pszName)))
        {
            out.open(pszName);
            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)