Because in Windows, with Visual Studio, the __FILE__
macro can give lower-case strings, and because I need to know the correctly-cased filename (as perceived by users), I am trying to work out how to convert an all lower-case path on windows to the correctly cased one.
It looks like using stat to get info about the file may be a good starting point.
But how, given a stat object, can I get the filename?
#include <sys/stat.h>
#include <string>
void test()
{
std::string file_name = "C:\\WiNdoWs";
struct stat statbuf;
int stat_ok = stat(file_name.c_str(), &statbuf);
auto file_serial_number = statbuf.st_ino;
std::string full_file_name = ???;
}
Or is there any other C++11 way of achieving this? It only needs to work on Windows, so Windows-specific solutions (with code) are fine.
We would like to do this without adding a dependency on either boost filesystem or Qt, or moving to C++17.
Update 1
For context, the code is in a header-only testing framework, that intends to create some output files based on the name of the source file containing the test - so a test file called DemoTest.cpp
would generate an output file whose name begins DemoTest...
.
The problem we trying to solve is that, for some Visual Studio compiler settings, the output file name begins demotest...
.
So this means that the stat
call is running on the same source file where __FILE__
was generated at compile time.
Update 2
It turns out that the statbuf.st_ino
value is zero when debugging in Visual Studio 2017 - presumably because the value represents a Unix inode, and there's no such thing on Windows.
So this whole approach won't work.
Are there any other options?