Question About Directory

Started by
3 comments, last by Goran Milovanovic 11 years, 10 months ago
Hi everyone

I have a question about define resouce directory. My question is usually after compiling and link the project, it will produce an exe file and put it into either debug or release file. However when I run the exe file, it can not find files like resouces which are put into a file called res and it is at the project directory.

One solution is to make a copy of the res file and put it into debug or release file as well. But as the size grows it will not be a wise way. So any another solution?

Put some pieces of code here. This is probably not right.

char* LumPySystem::GetResoucePath (const char* acFilename)
{
char* Location = new char[50];
#ifdef _DEBUG
char* dir = "res/";
int nLen = strlen(Location) + 1;
strcpy_s(Location,nLen,dir);
strcat_s(Location,nLen,acFilename);
#else
char* dir = "../res/";
int nLen = strlen(Location) + 1;
strcpy_s(Location,nLen,dir);
strcat_s(Location,nLen,acFilename);
#endif
return Location;
}
Advertisement
Can you describe your directory structure in a little more detail?

I mean, where are the executables located for those two cases? Relative paths are relative to the directory from which the program is called, so you should double check the correctness of dir for either case.

PS: strcpy_s is not standard C++, afaik.

+---------------------------------------------------------------------+

| Game Dev video tutorials -> http://www.youtube.com/goranmilovano | +---------------------------------------------------------------------+
My project location is C:\Users\Jerry\Desktop\LumPy inlcuding:
LumPy
LumPy.ncb
LumPy.sln
The file of LumPy includes all my cpps and header files plus:
Debug
Release
Res
In Debug file there are just obj files and an exe

So in the programme I use Res/ as my resouce directory but if I run the exe file in debug for example it can not find the resouce. What am I suppose to do to make it automatically load the file in its parent's directory?

I was using strcpy. but VC alway reminds me to use strcpy_s. If it is not standard way. What else I can use? Thanks
If you're using C++ then why not use the string class (aka basic_string<char>)?

string location;

#ifdef _DEBUG
location = "foo";
#else
location = "bar";
#endif

location += filename;

Also, the VC is giving you a warning, not an error. Either ignore it or disable it using a pragma. Microsoft means well, but their recommended solution is utter garbage. So, why not use string? For the record, you should probably use wstring when handling file and directory names in general, but string may be fine in your specific case.

Look into the string's c_str() function to see how it interfaces with C-style functions. It returns a pointer, but don't return this pointer. Return the string, or pass it in as a reference so temporary duplicates are avoided.
I don't think you even need an #ifdef directive in this case, because looking at your directory structure, it seems like "../Res/" is the proper relative path .... assuming that Res is a directory, which contains the file in question.

Capitalization matters in this case, so be sure that's correct.

Also, I would like to echo the point made by taby: Since you're already using C++, you should use the C++ string.

+---------------------------------------------------------------------+

| Game Dev video tutorials -> http://www.youtube.com/goranmilovano | +---------------------------------------------------------------------+

This topic is closed to new replies.

Advertisement