# How to find where the application is running from

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

## 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 on other sites
I believe it's _getcwd

yep string test=_getcwd(NULL, 0);

piece of cake :D

##### 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 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 on other sites
Quote:
 Original post by knowyourroleThe 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 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?