C++ Relative Paths

Started by
12 comments, last by Xirion 11 years, 7 months ago
Good day.

The structure of my proyect is:

  • POOA

    • data
    • src
    • vcproj

      • holy



So my Visual Studio solution is in the folder "holy" and i need to use an image wich is in
the "data" folder so i try to use something like ..\\data\\ tiles.png but it's no use, how can i
accomplish this

SDL_surface *tile_sheet;
tile_sheet = load_image( "..\\data\\ tiles.png" );

Im using C++ and SDL
Advertisement
You need to use a path relative to the generated executable, not the project itself. What directory does your executable end up in when you build?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

vcproj/holy/debug

i tried using: "..\\..\\data/tiles.png" and it seems to work now, but is this correct or is there a better way?
You can replace the \\ with /, and your paths will work fine on Windows as well as Linux/Mac (That's not the cause of your problem though). Consistency is good.

Your path in the first post is:
load_image( "..\\data\\ tiles.png" );

If you copied and pasted that from your actual code, then the problem might be that you have a space between "data\\" and "tiles.png".
load_image( "..\\data\\[color=#ff0000]<space>tiles.png" );
Each '..' moves up one folder. So if your path is:
"POOA/vcproj/holy/debug/game.exe"

And you want to get to:
"POOA/data/tiles.png"

You need:
"../../../data/tiles.png"

(one '..' for each of: "vcproj/holy/debug")

However, more practically, your game isn't always going to be in the debug folder. If you release your games to other people, you'll more likely not want it in a folder labeled "debug".
So, you should tell Visual Studio to output the generated executable into a folder more to your liking. I suggest a 'bin' folder, right next to the 'data' and 'src' folders, as it is commonly named 'bin' (for 'binaries', which is what .exes and .DLLs are).

Once you tell Visual Studio to always put the executable in your 'bin' folder, each time you compile your project it will end up there. Then your path would just be "../data/tiles.png"

I don't use Visual Studio personally, so I can't tell you how to redirect the exe to a different folder, but I'm sure it can do it (most IDEs can).
Under the properties for your VStudio project: Configuration Properties->General->Output Directory is where your project will ultimately compile to. Then just make sure all your assets exist in that directory and you can use relative paths as you were.
Basically it doesn't matter where your binary ends up, as long as you start it from the debugger. The only thing that matters is what you configured as your working directory. Once you want to run it manually, you have to make sure the binary is located in that same directory.

Considering that your binary will most likely be in POOA when you're done, you may want to change the debugger settings to use that as your working directory (something like $(ProjectDir)/..) and stick with "data/tiles.png" as your path.
f@dzhttp://festini.device-zero.de
Thank you all for the tips, i'm sure i'll be using them from now on.
@Servant of the lord: the space was because if i don't use that space my letter "t" was erased.
another question though, how do i mark this topic as SOLVED?
You don't.

We don't use the notion of "solved" topics here, someone might always chime in a little later with an extra information. Just because the OP thinks they have an answer, doesn't mean that it is the best answer.

The Forum FAQ in fact specifically says not to do so.

This topic is closed to new replies.

Advertisement