__FILE__ without full path

The C++ macro __FILE__
typically includes the full path (with many compilers).
For my tracing, I want just the file name, not the full path.
Is there a built-in macro for this in any version of C++ or do I have to write my own?
This is specific to C++, so a question targeted at 'C' is not similar.
Answer
In C++20 or higher, the following macro implements the desired behavior.
It's a single line of code and works the same way that __FILE__
does.
#define __FILENAME__ std::string_view(__FILE__).substr(std::string_view(__FILE__).find_last_of("/\\") + 1).data()
Enjoyed this question?
Check out more content on our blog or follow us on social media.
Browse more questions