Hi,
Now ive got the dynamic memory sorted. Is there any function / class in the c++ std that returns the path of the working directory.(the path of the directory which the application is in)
Cheers
Alex
Printable View
Hi,
Now ive got the dynamic memory sorted. Is there any function / class in the c++ std that returns the path of the working directory.(the path of the directory which the application is in)
Cheers
Alex
One way is to use the first argument when defining main with command line arguments support. It gives you the full path of the executable.
Read http://www.cprogramming.com/tutorial/lesson14.html
>> It gives you the full path of the executable.
That is not necessarily true. Often only the executable name is available (as your link points out).
Getting the working directory is a platform specific operation, so what OS and compiler are you using?
well for the current project win32 visualc++Quote:
Originally Posted by Daved
Edit: I found a func: getcwd(), seems to work on linux, win32 & solaris
-Alex
getcwd() is a POSIX function, so it should work on quite a few systems.
>I found a func: getcwd()
Are you looking for the current working directory of the user, or the directory in which the executable resides?
Yup. My bad. And I just checked and mingw doesn't give the full path. So, I have no idea why I was convinced that to be true.Quote:
Originally Posted by Daved
http://msdn.microsoft.com/library/de...lefilename.asp
http://msdn.microsoft.com/library/de...lebasename.asp
one retrieves the path with the file name the other just the file name.. figure it out.
EDIT:
there...Code:TCHAR FileName[MAX_PATH] = {0};
TCHAR FilePath[MAX_PATH] = {0};
GetModuleFileNameEx(GetCurrentProcess(), NULL, (LPWSTR)&FilePath, MAX_PATH);
GetModuleBaseName(GetCurrentProcess(), NULL, (LPWSTR)&FileName, MAX_PATH);
size_t FP = wcslen((wchar_t*)&FilePath);
size_t FN = wcslen((wchar_t*)&FileName);
FilePath[FP-FN] = L'\0';
That's how you get the true name of the executable, but I think the OP was looking for getcwd():
Of course, you could call GetModuleFileName() and strip off the file name itself to get the directory.Quote:
Is there any function / class in the c++ std that returns the path of the working directory.
Note: these are NOT the same!Quote:
Originally Posted by appleGuy
You can change the working directory, or the program can start in a different directory than that which the executable is in. (Try it: right-click, create shortcut, right-click, properties and you can change the working directory.)
Which one you want (working or executable directory) depends on what you're trying to do.