In argv[0] is the full path to the executable file, and how to get the directory where the file is located?
- onefor each platform its own way to determine the directory you need for what? - ampawd
- OS Windows Platform - Vitali
|
2 answers
for example, in this way
#include <iostream> #include <string> int main(int argc, char** argv) { std::string argv_str = argv[0]; std::string base = argv_str.substr(0, argv_str.find_last_of("\\")); std::cout << base << '\n'; return 0; } - Under Windows, the path separator symbol may depend on national settings. (Hello Korea!) Even under Russian windows argv [0], it may contain "slashes in the wrong way", for example, start your program with the command
./test.exefrom under far. - Chorkov - @Chorkov well for such slashes it can be done in the same way, this is not the point - ampawd
|
Position at the end of the line, moving in it with a pointer in the opposite direction to find the first occurrence of the / character from the end (or \ for Windows - it is important to check in the debugger which character was written). Moving towards the beginning we will find the second occurrence of the symbol from the end. The directory will be between these pointers (up to what the pointer indicates).
|