Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ windows fstream case sensitive

Tags:

c++

fstream

I'm noticing that on Windows, file opening is case insensitive.

(ex. fstream("text.txt") will open regardless of the actual filename being: Text.txt)

How would I make this case sensitive instead? (The file not opening unless the filename also matches the proper case)

like image 205
dk123 Avatar asked Sep 16 '26 08:09

dk123


1 Answers

Under Windows the file system API is generally case-insensitive, so the only way is to check the case of the filename yourself. For example,

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;
}
like image 154
Jonathan Potter Avatar answered Sep 17 '26 23:09

Jonathan Potter