How do I set a programs search directories?

Basically, How do I tell my program to look in a certain place for any files (images, data, etc.) that it needs to run so that I can move the program from computer to computer and it still works? This way I won't have to open up code::blocks and rebuild a program every time I want to use it. For Example, if I have my images in a folder in the same directory as the program itself, how can I make the program look for the images inside that folder? Any help is appreciated.
Wherever you refer to the image names inside your program, simply adjust them to add the path in front of the name. That'll probably work.
The problem with that is that if the program gets moved to a new computer, or just location, then I will have to change the path. I'm trying to avoid that. Is there any function or setting that automatically sets the current search directory to the folder where the program is located without having to directly specify what that directory is.
Is there any function or setting that automatically sets the current search directory to the folder where the program is located without having to directly specify what that directory is.


That is the default, if you don't set a path, it looks in the current folder where the exe is located, if the path is <currentfolder>\pictures then you would tell it "pictures\filename.ext".
Last edited on
Are you sure about because I used _getcwd(not sure if that's the correct name since I'm typing from my phone and can't check) to check the current directory at the beginning of my program and it said it was the bin folder where I have code::blocks installed. I even put the picture there to test it which is why I got confused
Last edited on
bump
There are multiple ways to specify that you want to work in the directory of your program. Some example paths:
1
2
3
4
  "filename.ext"
"./filename.ext" //identical to above
  "pictures/filename.ext"
"./pictures/filename.ext" //identical to above 


The example posted by Samuel Adams is wrong because a path starting with a slash means it starts from the current drive. So "/pictures/filename.ext" would be "C:/pictures/filename.ext" if the program was started on drive C.

You should also note that even on Windows you can sue forward slashes in paths, it will accept them without an issue. If you use backslashes you have to escape them and it looks ugly: "pictures\\filename.ext"
Last edited on
Ok thanks; however, upon further research it seems that the problem I described isn't what's stopping me from running my programs directly from the program itself. I'm not sure what it is other than that my program isn't loading the files even though I made sure it's using the right directory and the files are in that directory.
Topic archived. No new replies allowed.