• 13
• 16
• 27
• 9
• 9

# C++, file pathing problems

This topic is 2805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a problem grabbing the correct path for my application's media files. Basicly I have it like this:

w32/app.exe
w32/media/media.tga
w32/media/media.x

ect...

for some reson the directX functions that need the paths, dont take dos pathing, so something like this wont work: \\media\\media.tga or ..\\media\\media.tga wont do it either. So I need the abosulte path. I need to get the path of where the .exe is started from and add the addition text to it. Any methods of doing this?

the pathing a LPCWSTR.

##### Share on other sites
You can use GetModuleFileName to retrieve the full path (plus filename) of the .exe you're in. Remove the file name and build your relative path from there.

The DirectX functions should be able to cope with relative paths, maybe it's because your working directory is not where you expect it to be. Have you checked the current working directory if the calls fail to load?

##### Share on other sites
So far ive done this:

	TCHAR szPathName[_MAX_PATH];	::GetModuleFileName(NULL, szPathName, _MAX_PATH);	LPTSTR pszFileName = _tcsrchr(szPathName, '\\') + 1;	*pszFileName = '\0';

it returns my path to the .exe without app name.
Now how can I add "Media\\filename.format" to szPathName and convert this to a LPCWSTR.

Edit @ Endurion:
DX doesnt work with relative paths for some reson. I have to type in the absolute path, then it reads the files and loads them. Anyway, the main reson im doing this is because it needs to work on multiple computers.

##### Share on other sites
I would assume DX works perfectly fine with relative paths, the current working directory is more likely just not what you expect it to be. The default working directory when using MSVC for example is the directory which contains the project file while the actual .exe is created in the Debug or Release subfolders.

Since nothing else is specified I would assume you are using C++, so my advice would be to move over to std::string/std::wstring as soon as possible and especially for the needed string manipulation. In this case something like
assetPath = myAppPath + "subfolder\\something.wtf"
should do.

Personally I would greatly prefer working with boost::filesystem::path/boost::filesystem::wpath though.

##### Share on other sites
A different way of doing it is this.

char buffer[1024]; _getcwd(buffer, 1024);

The benefit of this is that it will also pick up the current working directory you setup in visual studio projects or any other IDE. Plus its cross platform and ISO C++, where GetModuleFileName is very WIN specific.

##### Share on other sites
Ok, I found a way of doing this sorta, but im getting a error:

CResourcePool.obj : error LNK2001: unresolved external symbol "public: wchar_t const * __thiscall CResourcePool::SetPath(wchar_t *)" (?SetPath@CResourcePool@@QAEPB_WPA_W@Z)

LPCWSTR SetPath(const wchar_t *File){	TCHAR szPathName[_MAX_PATH];	::GetModuleFileName(NULL, szPathName, _MAX_PATH);	LPTSTR pszFileName = _tcsrchr(szPathName, '\\') + 1;	*pszFileName = '\0';	return reinterpret_cast<LPCWSTR>(_tcscat(szPathName, File));}

and its used like this:

D3DXLoadMeshFromX(SetPath(_T("tree.x")),                      D3DXMESH_MANAGED,                      g_pD3D->m_pD3DDevice,                      NULL,                      &TreeMatBuffer,                      NULL,                      &TreeNumMaterials,                      &TreeMesh);

##### Share on other sites
Add CResourcePool:: to SetPath where you define it.

Using TCHARS together with wchars? Hm, something is really needless.

##### Share on other sites
Haha yea, sry bout that xD and epic fail on my end, completely forgot to add that..., thanks it works now.

LPCWSTR CResourcePool::SetPath(TCHAR *File){	TCHAR szPathName[_MAX_PATH];	::GetModuleFileName(NULL, szPathName, _MAX_PATH);	LPTSTR pszFileName = _tcsrchr(szPathName, '\\') + 1;	*pszFileName = '\0';		return reinterpret_cast<LPCWSTR>(_tcscat(szPathName, File));}

LPCWSTR CResourcePool::SetPath(TCHAR *File){	TCHAR szPathName[_MAX_PATH];	::GetModuleFileName(NULL, szPathName, _MAX_PATH);	LPTSTR pszFileName = _tcsrchr(szPathName, '\\') + 1;	*pszFileName = '\0';		return reinterpret_cast<LPCWSTR>(_tcscat(szPathName, File));}