Discussion:
Relative Path
(too old to reply)
Jack
2009-12-04 11:11:50 UTC
Permalink
Hi gurus,
I am trying hard on this, but with success.
I'd like to get the relative path \\data\\Demo.dat

void OpenFile (char *filename)
{
char szPath[256];
GetCurrentDirectoryA(sizeof(szPath), szPath);

SetCurrentDirectoryA(szPath);

strcat (szPath, filename);


FILE *fp = fopen(szPath, "rb");

//////////////////////////////
When I put \\data\\Demo.dat there as the parameter.
The file being referenced is
C:\Current Path\Project\Project\data\

When I place \\..\\data\\Demo.dat there
it works but I don't like it that way
C:\current Path\Project\data

As I don't want to hard code it,
How can I make it always relative to the exe file....
Sorry as it has been asked for trillion times.
Thanks
Jack
Jack
2009-12-04 11:20:42 UTC
Permalink
No, without success :)
David Wilkinson
2009-12-04 12:11:36 UTC
Permalink
Post by Jack
Hi gurus,
I am trying hard on this, but with success.
I'd like to get the relative path \\data\\Demo.dat
void OpenFile (char *filename)
{
char szPath[256];
GetCurrentDirectoryA(sizeof(szPath), szPath);
SetCurrentDirectoryA(szPath);
strcat (szPath, filename);
FILE *fp = fopen(szPath, "rb");
//////////////////////////////
When I put \\data\\Demo.dat there as the parameter.
The file being referenced is
C:\Current Path\Project\Project\data\
When I place \\..\\data\\Demo.dat there
it works but I don't like it that way
C:\current Path\Project\data
As I don't want to hard code it,
How can I make it always relative to the exe file....
Sorry as it has been asked for trillion times.
If you know the location relative to the executable, you should use
::GetModuleFileName(NULL, ..), not ::GetCurrentDirectory(). In general you will
not know in advance what the working directory is.
--
David Wilkinson
Visual C++ MVP
Jack
2009-12-04 12:42:24 UTC
Permalink
Yes, great I've got it
Thanks David for your kindred help
Jack

Continue reading on narkive:
Loading...