我注意到在 Windows 上,文件打开不区分大小写。
(例如 fstream("text.txt")
将打开,无论实际文件名是:Text.txt
)
我该如何让这个区分大小写呢? (文件不会打开,除非文件名也匹配正确的大小写)
最佳答案
在 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;
}
关于C++ Windows fstream 区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29248797/