Sign in to follow this  
ViperG

How to find where the application is running from

Recommended Posts

is there a c++ command for a win32 program to get the filepath that the application is running in? i want to do system("notepad app_location/myfile.txt") I can't just do notepad c:\blah\blah because the app can be placed anywhere on the hd.

Share this post


Link to post
Share on other sites
The first command line parameter passed to every program is the program's absolute location, so you can just get that quite easily.

Example program:

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
std::string path = argv[0];
std::cout << path << std::endl;
return(0);
}


edit: This doesn't mean you have to pass it any command line parameters by the way, it's kind of like a default parameter that is always passed.

Share this post


Link to post
Share on other sites
I dont think C++ has support for that.
It require system speciffic code so it is implemented as a system call.
You should find getcwd in the unistd.h header, or you can use the Windows API equivalent
GetCurrentDirectory

If you want some serious control over file and directory operations, you might want to look into the Boost filesystem module.

edit:
poking into argv might work too. Didn't think of that

Share this post


Link to post
Share on other sites
Quote:
Original post by knowyourrole
The first command line parameter passed to every program is the program's absolute location, so you can just get that quite easily.


No, the first command line parameter is the name of the program (if it's available) or a null character ('\0') if it's not.

However, unless I'm quite wrong, the parameter passed to notepad is evaluated according to the standard rules. (i.e. current working directory, then the PATH environment variable).

So if you have, say,

C:/Program Files/myprog.exe
C:/Program Files/data/mydata.txt

Then you should be able to do system("notepad ./data/mydata.txt").

Share this post


Link to post
Share on other sites
that would be cool lemme test it

yeah that worked too, so i dont need the cwd. except system makes my program hang, whats that other command besides system...execute?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this