   Loading resource files using absolute path.
  Hello,I've made a small SDL game in C++, running it from within my IDE (Eclipse with CDT on Ubuntu Linux 7.04) or browsing to the directory with the data files in (.png's, .ogg's, .ttf's etc...) in the terminal (console) and executing it from there works fine.But if I double click on it in my file browser (Konqueror) it loads briefly and then closes, presumably because the files cannot be loaded such as "fonts/FreeSans.ttf" or "images/myimage.png" because it's looking somewhere else for the base directory. You get the picture...I'm going to guess that the way to fix this is by calling LoadImage("/absolute/path/to/file/images/myimage.png") rather than LoadImage("images/myimage.png"), but of course, if there is a better way, please let me know.The problem is: how do I find out where my game is being ran from at run time?Thanks in advance,Danny   
  If I remember correctly, the location of your binary is stored in argv[0]. Try couting it in a test project, I bet it'll be helpful in a way. I don't have a compiler right now to check it..   ;  Excellent, that's exactly what I was after. How very simple!Yes, it returns the absolute path to the executable.E.g. /home/danny/eclipse/Test/Debug/Test.outSo I just need to strip the string of all the characters after the last '/' and then prefix my paths with it.This works:std::string path = argv[0];path.erase( path.find_last_of("/") );Thanks,Danny   ;  Personally I'd look for a setting in my IDE where I can modify the starting directory. Saves you quite some work as you can just keep using relative paths. I don't know where you can set it in Eclipse though, probably in the compile or project settings somewhere.   ;  It works fine when I run it from within eclipse. I'm just talking about when I run it from my file browser.Say I send it to a friend, they would have to execute it from the console, making sure they have changed to the directory that all my relative paths are relative from.If there is a better way to accomplish this, by all means let me know.Thanks,Danny   ;Quote:Original post by c4c0d3m0nIf I remember correctly, the location of your binary is stored in argv[0]. Try couting it in a test project, I bet it'll be helpful in a way. I don't have a compiler right now to check it..That is not true. It might work in some cases, but in some other cases it might fail.The argv[0] is the command, used to run your application (same for windows and linux). So, for example, if your executable file is myapp and you run it from the command line by simply typing myapp, then argv[0] will also be equal to "myapp". If you run it by entering the command ~/myapp - that's the string you will find in argv[0].Now, when you run the file from some window manager - it usually runs the application by specifying full absolute path to the file, therefor it works for you. But you should be aware that argv[0] might not work in some other cases.I'm not a Linux expert, so I'll leave it to someone else to tell you what is the correct way to find out the absolute path of your executable. In Windows, I'd be using GetModuleFileName function.   ;  But am I on the right lines? Is this the method you would use to load resource files?If it is, and this is going to be an OS specific solution, that may be a problem. I'm using SDL for it's cross-platform features. I've had a look through the SDL Wiki and it doesn't seem to have any way of finding the path to the executable.If anyone uses a completely different method, please let me know :)Thanks for all your responses,Danny   ;  I'm not sure how it's with eclipse, but i had similar issue with codeblocks. The problem was that if you have your project sitting in folder "my_project" then you have your game data - for example - in "my_project/data". And it works fine if you run your project from IDE since executable is run from "my_project". However the compiled file is put in "my_project/bin"(depends on IDE). So if you click it, it will search for data in "my_project/bin/data". s simply copy data(or whatever it's name is) folder into bin or wherever your executable file is after compilation.Once again: it works so with codeblocks but i think it's pretty similar in eclipse.   ;  You can find the location of the current executable by using readlink on /proc/self/exe if you have the proc filesystem available.Check 'man 2 readlink', run that using '/proc/self/exe'.Here's a quick snippet I just made to make sure it works:#include "unistd.h" /* for readlink() */#include "stdio.h"int main(){    char buf[255] = {0};    if(readlink("/proc/self/exe",buf,255) != -1){printf("%s\n",buf);    }else{/* Path resolution failed */    }}It outputs the absolute path and name of the executable. Obviously this is not cross-platform, but it works on any *nix system with proc.I use PhysFS to overcome that issue, which also came with a few nice perks for packaging the data. Only real downside is you need to extract the byte array of the file data using the library, so you can't pass a filename to another library. Pretty much all libraries I've come across do accept both filename and byte array input, though.   