# How to load game resources correctly regardless of the working directory?

I am writing a resource manager for a hobby game project that reads textures/fonts etc.

Up to now, I was giving the location of the files relative to the executable path, like:

my_texture.loadFromFile("textures\\my_texture.png");


However, now I realized that the relative path I'm giving is relative to the working directory of the game. So, for example, if I run the executable from my IDE (which sets the working directory to the project directory instead of executable directory), the game fails to load the texture and crashes.

I was thinking about using the main's argv[0] and did some research on it, but apparently it is not reliable (it might just give the name of the exe).

So, how can I get the path to my resource files reliably? How does the professionals do it?

I'm using the SFML and C++17. I would like a portable solution if possible, otherwise Windows.

I checked this question but it wasn't helpful.

Give the resource management API an understanding of what the input paths are relative to. Depending on your underlying OS, there are a variety of ways to get the full path to the running executable, independent of working directory. On Windows, for example, one function you could use for this is GetModuleFileNameA.
SFML may have a portable wrapper equivalent to GetModuleFileNameA, although I'm not sure what it is offhand and a very quick search through the API reference didn't reveal anything.
• @MarkStorer boost::filesystem::current_path() returns the current working directory, which might be often different from the executable's directory May 8 '19 at 15:48