You can't, std::ifstream
does not store this information.
What you can do, however, is:
use a library like Boost.Filesystem library to transform between relative and absolute paths.
boost::filesystem::path abs_path = boost::filesystem::complete("./rel/path");
std::string abs_path_str = abs_path.string();
The fstream classes have no functionality for accessing or processing the name used to open the file with, and the C++ Standard Library has no filename processing functions - you will have to write the code yourself, or use a third-party library or operating system-supplied functions.
I don't think it is possible for a std::fstream. I did it for a FILE * on Windows (in a non-portable way). See from file object to file name.
Have you considered extending the ifstream with your own class that remembers the file name?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With