Click to See Complete Forum and Search --> : obtain current file directory


rssmps
10-07-2005, 02:52 PM
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!

Danny
10-07-2005, 05:55 PM
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");

rssmps
10-07-2005, 06:10 PM
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!

Danny
10-07-2005, 08:22 PM
they aren't officially standard but in practice, both Windows and POSIX systems support them, so they are pretty portable.