Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

obtain current file directory

I'm currently using the following code to obtain the current directory.
is there a better C++ way of doing this? is there such a thing as #include <direct>?

#include <direct.h>

char buffer[_MAX_PATH];
/* Get the current working directory: */
if( _getcwd( buffer, _MAX_PATH) == NULL)
{
cout<<( "_getcwd error on file" + filein );
exit(0);
}
else
{
path = buffer; // path is string
}


also, for compatibility between unix and win, is there anythin in C++ such as the following in Java?
System.getProperty("user.dir")
System.getProperty("file.separator")

Thanks!
[692 byte] By [rssmps] at [2007-11-11 6:59:57]
# 1 Re: obtain current file directory
There's "dir.h" and "dirent.h" but they both use getcwd(). Alternatively, you can use the getenv() function to obtain the current directory, if you you its symbolic name:

const char * pdir= getennv("path");
Danny at 2007-11-11 21:02:51 >
# 2 Re: obtain current file directory
I'll give these a try...and report back if there are any problems.

for the code that I have above and the dir.h/dirent.h standard libraries, are these libraries win specific or standard across platforms?

Thanks!
rssmps at 2007-11-11 21:03:46 >
# 3 Re: obtain current file directory
they aren't officially standard but in practice, both Windows and POSIX systems support them, so they are pretty portable.
Danny at 2007-11-11 21:04:50 >