How to find where the application is running from

Started by
4 comments, last by ViperG 17 years, 1 month ago
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.
Black Sky A Star Control 2/Elite like game
Advertisement
I believe it's _getcwd

yep string test=_getcwd(NULL, 0);

piece of cake :D
Black Sky A Star Control 2/Elite like game
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.
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
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").
[TheUnbeliever]
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?
Black Sky A Star Control 2/Elite like game

This topic is closed to new replies.

Advertisement