Advertisement

fstream w/ relative file paths??

Started by August 03, 2000 07:22 AM
4 comments, last by t2sherm 24 years, 4 months ago
i have a function that loads a level, and it uses fstream. Like this: char Buffer[50]; int temp; sprintf(Buffer, "D:\\Pong\\Release\\Level%d.lev", Level); fstream file; file.open(Buffer,ios::in); // open the input file file >> temp; but If i just put a relative path for the file.open first parameter, it doesn''t work. But when i put the whole path in it works, and I was wondering if there was any way to get it to work with relative paths?? How am I supposed to distribute this game, I won''t know where it is on their hard drive, and even if I could figure it out, there should be an easier way! t2sherm ô¿ô
t2sherm ô¿ô
In which directory is your .exe file??

GA
Visit our homepage: www.rarebyte.de.stGA
Advertisement
Remember, if you are in the msvc++ develop enviroment, it is relative to the project dir, and not where the exe is !!!
Ries
Thanks! I had it in the release directory, where it compiles to ,but when i run it with the execute command in the IDE, it doesn't work, but I just tried it without being in the IDE, and it works! So I moved the files to the project directory. Thanks for your help!

t2sherm ô¿ô

Edited by - t2sherm on August 3, 2000 1:21:06 PM
t2sherm ô¿ô
What is probably a better solution is to set the current working directory to that of the exe file in the program initialization. This way, if they run it from a command line or something, the current directory will always be correct.

Here''s some code:

    // set the current working directory to the directory of the exe file	// code from the CDX Application Wizard by Michael Rich, istan@alltel.net	char buf[MAX_PATH];	char *cptr;	//now change the directory	//to make sure were accessing the proper file	GetModuleFileName(NULL, buf, MAX_PATH);	//move pointer to end of string	cptr = buf + lstrlen(buf);	//find the end of the path	do	{		cptr--;	} while (*cptr != ''\\'');	cptr++;	*cptr=''\0'';	//change directory	SetCurrentDirectory(buf);    
the best thing to do is to simply set your exe to be made in another dir (the right one!)
in mscv6 use: Project>Settings>Link tab>Output file name

ussually i just stick with defualts though... that way you put all media in project root dir (ie not in debug/release subdir) then both debug/release executables can access the media with no code change. then when the time comes to put the program in it''s own folder, you put the exe in the same dir as the media dir(s) and everything is ok.

This topic is closed to new replies.

Advertisement