Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

siddim

current working directory?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, is there a function that can give me the current working directory in which the .exe was executed? thanks

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
int main(int argc, char* argv[])
{
int At=0;
char Dir[MAX_PATH];

for (int i=strlen(argv[0])-1; i>=0; i--)
{
if (argv == ''/'' || argv[i] == ''\\'')
{
At = i;
break;
}
}
strcpy(Dir,argv[0]);
Dir[At] = 0;
cout << Dir;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
oops...


  
int main(int argc, char* argv[])
{
int At=0;
char Dir[MAX_PATH];

for (int i=strlen(argv[0])-1; i>=0; i--)
{
if (argv[i] == ''/'' || argv[i] == ''\\'')
{
At = i;
break;
}
}
strcpy(Dir,argv[0]);
Dir[At] = 0;
cout << Dir;
}

Share this post


Link to post
Share on other sites
I had some trouble with this myself, but I have written three functions that take care of it (on Win32, Linux and Mac OS X). They use strings though, so if you wanted to go the C route this probably wouldn''t be for you.


  
// WIN32 version

std::string getCurrDir(char *argv)
{
std::string sDir = argv;
sDir = sDir.substr(0, sDir.find_last_of(''\\'')) + ''\\'';

return(sDir);
} // end getCurrDir() WIN32 version


// Linux version

std::string getCurrDir(char *argv)
{
std::string sDir = argv;
sDir = sDir.substr(0, sDir.find_last_of(''/'')) + ''/'';

return(sDir);
} // end getCurrDir() Linux version


// Mac version

std::string getCurrDir(char *argv)
{
std::string sExecName = argv;
sExecName = sExecName.substr(sExecName.find_last_of(''/''), sExecName.length());

std::string sDir = argv;
sDir = sDir.substr(0, sDir.find(sExecName + ".app")) + ''/'';

return(sDir);
} // end getCurrDir() Mac version



And then to use it you would do this:

  
#include<iostream>

int main(int argc, char *argv[])
{
std::string currDirectory = getCurrDir(argv[0]);
// ...

return(0);
}


Hope this all helps!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Getting the application directory out of argv[0] is really, really wrong. In many ways.

The right thing to do is to use your shared library API and figure out where your main module is coming from; most decent OSes (including Windows) will let you ask for the full path to a module by name or address.

Check GetModuleFileName() on msdn.microsoft.com if you''re on Windows.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!