How to find where the application is running from
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.
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:
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.
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
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").
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement