Consider this line:
const auto file = juce::File::getCurrentWorkingDirectory().getChildFile ("\\hello.txt");
The intent is to create a path to a file named “hello.txt” at the root of whatever drive (“C:”, “D:”) the current working directory is on.
The problem is that on Windows, the above code results in an assertion in File constructor, because:
- getChildFile() checks if its argument is an absolute path with isAbsolutePath()
- isAbsolutePath() sees a directory separator at the beginning of the path, and returns true
- getChildFile() then proceeds to interpret the argument as an absolute path, constructing a File() with it
- The called File() constructor hits the assertion, where it is explained that the passed argument is not an absolute path as it should have, and to resolve the issue suggests exactly the code that caused the assertion.
When you supply a raw string to the File object constructor, it must be an absolute path.
If you’re trying to parse a string that may be either a relative path or an absolute path,
you MUST provide a context against which the partial path can be evaluated - you can do
this by simply using File::getChildFile() instead of the File constructor. E.g. saying
“File::getCurrentWorkingDirectory().getChildFile (myUnknownPath)” would return an absolute
path if that’s what was supplied, or would evaluate a partial path relative to the CWD.
The problem IMHO is that, at step #2 above, isAbsolutePath() should have returned false for “\hello.txt”, because on Windows that’s a relative path: it lacks the drive letter.