Hi, I'm looking for a code that gets the directory where the file is being executed (for example: C:\some\directory) and the name of the file that is being executed.(for example: someprogram.exe)
Thanks in advance.
Hi, I'm looking for a code that gets the directory where the file is being executed (for example: C:\some\directory) and the name of the file that is being executed.(for example: someprogram.exe)
Thanks in advance.
In windows there is GetCurrentDirectory
In linux maybe this
For the program name you can do int main(int argc, char** argv) and read argv[0]. Which is a char array that contains the name of the executable. But NOT always. But you can try it. I believe it is true at least for windows and some linux OS.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
That assumes that the environment variable PWD is actually in sync with the current directory.
Note also that an application can be started in a different directory than the application actually lives in, so if you want to know what directory the application actually lives in, it's can be difficult. In a well-behaved system, argv[0] contains the application name and location, but there's nothing preventing someone from writing code like this (in Linux/Unix):
argv[0] will then be "rubbish", but the application is "someapp" and loaded from "/somepath".Code:execl("/somepath/someapp", "rubbish", "arg1", NULL);
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thanks for all of your answers. One thing I forgot to mention is that I'm using C, not C++.
The code in fact was very simple, and is made, as you said, using GetModuleFileName. Here is the code:
Thanks for all of your help =DCode:#include <stdio.h> #include <stdlib.h> #include <windows.h> int main() { char path[100]; GetModuleFileName(NULL, path, sizeof(path)); printf("%s\n", path); system("pause"); return 0; }