I know that running a program through an environment such as Visual C++ or CodeBlocks will automatically set the working directory to whatever you choose, if you set it up correctly. Your IDE is probably set up to always run your Debug program straight from the /bin/ folder, but when you run it directly as an EXE, it's running it from the folder it's in.
I've had a similar problem in the past which caused me many headaches with my programs. It turns out that clicking and dragging a file over an EXE in Windows will change the working directory, and this prevented my program from finding vital files in the same directory. I eventually had to write a function that would force it to load files from the directory it's in.
Tumlee
Junior Poster in Training
97 posts since Oct 2011
Reputation Points: 59
Solved Threads: 16
That is very similar to how I have solved my problem, the only difference is that I used C-style strings and I got the directory of the executable from argv[0], and I avoided using Windows-specific functions because I had to cross-compile it eventually. What I ended up with was this, if you're curious:
char* get_exe_dir(void)
{
static char* exedir = NULL;
if(!exedir) //Generate the exedir by looking at argv[0]
{
//Search for the last directory seperator.
int slashpos = -1;
for(int i = 0; global_argv[0][i] != '\0'; i++)
{
if(global_argv[0][i] == '/' || global_argv[0][i] == '\\')
slashpos = i;
}
//This should work even if slashpos == -1
exedir = new char[slashpos+2];
memcpy(exedir, global_argv[0], slashpos+1);
exedir[slashpos+1] = '\0';
//For consistency, change all backslashes to forward-slashes.
//All operating systems, including Windows, support forward-slash as a dir separator.
for(int i = 0; exedir[i] != '\0'; i++)
if(exedir[i] == '\\') exedir[i] = '/';
}
return exedir;
}
Tumlee
Junior Poster in Training
97 posts since Oct 2011
Reputation Points: 59
Solved Threads: 16