#### Archived

This topic is now archived and is closed to further replies.

# working directory constantly changing?

## Recommended Posts

I have been having serious trouble with file management throughout all the time working on my game. what seems to be happening is files are loaded/saved into a directory, then all files attempting to use relative directories use that has their working directory. for example, I open/save my Config.ini file without any directory information. because of this, my ini file gets deposited anywhere a file has been opened/saved last. surely, there is a way to prevent the working directory from changing like this?

##### Share on other sites
er, how are you opening and closing files?

##### Share on other sites
I use std::fstream/ifstream/ofstream, usually I just let it go out of scope, but there are a few places where I .close it. I don't do anything strange with opening or closing files, and I've checked the filenames EVERY SINGLE open call makes with my log, all seem perfectly fine.

[edited by - billybob on January 22, 2004 9:57:50 PM]

##### Share on other sites
use the full path when you save your files.

##### Share on other sites
Then it will only work if its extracted to one specific directory.

##### Share on other sites
// set current directory to exe's rootwchar_t applicationDir[MAX_PATH];::GetModuleFileName(instance, applicationDir, MAX_PATH);wchar_t* endPath = wcsrchr(applicationDir, L'\\');if(endPath)	*endPath = 0;::SetCurrentDirectory(applicationDir);

[edited by - antareus on January 22, 2004 10:28:26 PM]

##### Share on other sites
I already do that antareus, but in order to fix my problem I''d have to do that every time I open a file (the SetCurrentDirectory part, at least)

##### Share on other sites
Can''t you just operate off of relative paths when you know you''re in the application''s directory?

##### Share on other sites
I don''t understand what you mean. first of all, how would you know, second, what would I do when I''m NOT in the application''s directory (as it seems to be after I open a file that isn''t in there)

##### Share on other sites
use all but the last line of antareus'' sample code, and save it in a char array somewhere. then, when you want to open a file, copy this saved path before the filename. you can even write an inline function to do it.

##### Share on other sites
That will work sometimes, but not if I use ..\ (not a problem, but it would be nice to have). I''d like to know why this happens, AFAIK, most people don''t have this problem.

##### Share on other sites
Because most people will simply getmodulefilename once and build full paths from there. The safest method ever.
If you insist on having ..\ you could always use a path class that will concatenate strings for you to build a qualified path string from that.

##### Share on other sites
Using ..\ in a path is allowed, so just copying the apps path before the filename should work.
"C:\windows\..\autoexec.bat" for example will refer to "C:\autoexec.bat", and the conversion will be handled by the OS

• ### Forum Statistics

• Total Topics
628342
• Total Posts
2982177

• 9
• 24
• 9
• 9
• 13