C++ Windows fstream 区分大小写

标签 c++ fstream

我注意到在 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/

相关文章:

c++ - cpp字节文件读取

c++ - std::fstream 缓冲与手动缓冲(为什么手动缓冲有 10 倍增益)?

c++ - 指针在 OpenMP 并行部分中是私有(private)的吗?

c++ - 复制构造函数产生不正确的旧数组拷贝

c++ - RSA ... GNU MP : Cannot allocate memory (size=1307836444)

c++ - 如何在 C++ 中推断函数参数类型?

c++ - 定时功能 : Double Returning 0 MS

c++ - 从命令行输入文件

c++ - 使用 ios::Nocreate 标志会导致 "undeclared identifier"错误

c++ - 使用 C++ 修复 .txt 文件中的列宽