I would recommend reading a book on C++ before you go any further, as it would be helpful to get a firmer footing. Accelerated C++ by Koenig and Moo is excellent.
To get the executable path use GetModuleFileName:
TCHAR buffer[MAX_PATH] = { 0 }; GetModuleFileName( NULL, buffer, MAX_PATH );
Here’s a C++ function that gets the directory without the file name:
#include <windows.h> #include <string> #include <iostream> std::wstring ExePath() { TCHAR buffer[MAX_PATH] = { 0 }; GetModuleFileName( NULL, buffer, MAX_PATH ); std::wstring::size_type pos = std::wstring(buffer).find_last_of(L"\\/"); return std::wstring(buffer).substr(0, pos); } int main() { std::cout << "my directory is " << ExePath() << "\n"; }
Related Posts:
- outputting ascii table in C++
- finding dll for “The specified module could not be found”
- How can I clear console
- How to go from fopen to fopen_s
- undefined reference to WinMain@16 (codeblocks)
- “X does not name a type” error in C++
- Windows 7 exception code: 0xc0000409
- Difference between ‘strcpy’ and ‘strcpy_s’?
- make: g++: Command not found
- VC++ fatal error LNK1168: cannot open filename.exe for writing
- MinGW: “gcc is not recognized as an internal or external command”
- LNK1168: cannot open debug\file.exe for writing
- VC++ fatal error LNK1168: cannot open filename.exe for writing
- How do I add the MinGW bin directory to my system path?
- Run C++ in command prompt – Windows
- Cannot open Windows.h in Microsoft Visual Studio
- The program can’t start because libgcc_s_dw2-1.dll is missing
- Is there a replacement for unistd.h for Windows (Visual C)?
- CMake : C and CXX compiler identification is unknown Win10 CMakeGUI
- What is the source of the data for the ProgramFiles, ProgramW6432Dir, ProgramFilesDir (x86), CommonProgramFiles environment variables?
- LPCSTR, LPCTSTR and LPTSTR
- REGSVR32: the module “xxxxx.dll” failed to load … dependent assembly could not be found
- How to create a dynamic array of integers
- How to implement 2D vector array?
- What is the difference between g++ and gcc?
- What is the best way to use a HashMap in C++?
- What is the difference between float and double?
- Struct Constructor in C++?
- What is meant with “const” at end of function declaration? [duplicate]
- What’s the C++ version of Java’s ArrayList
- What is the difference between “long”, “long long”, “long int”, and “long long int” in C++?
- ‘g++’ is not recognized as an internal or external command, operable program or batch file
- How do I convert a double into a string in C++?
- How to create timer events using C++ 11?
- How to concatenate two strings in C++?
- ‘g++’ is not recognized as an internal or external command, operable program or batch file
- Read file line by line using ifstream in C++
- Virtual/pure virtual explained
- error: redefinition of class
- Officially, what is typename for?
- What does T&& (double ampersand) mean in C++11?
- C++ – No matching member function for call to ‘push_back’
- error: lvalue required as unary & operand
- C/C++ JSON parser
- C++ Parsing input string to variables
- ‘cout’ was not declared in this scope
- Segmentation fault error 11 C++
- Why doesn’t C++ have a garbage collector?
- How does ifstream’s eof() work?
- terminate called after throwing an instance of ‘std::invalid_argument’ what(): stoi
- How to find memory leak in a C++ code/project?
- Printing the correct number of decimal points with cout
- libstdc++-6.dll not found
- vector
::size_type in C++ - What does ‘\0’ mean?
- cin >> “no operator matches these operands”
- C++ on mac : linker command failed with exit code 1 (use -v to see invocation)
- Correct way to use cin.fail()
- Destructor for a linked List
- Why use a new call with a C++ ‘vector’?
- Identifier is undefined
- Arduino sprintf float not formatting
- How to end C++ code
- Delete 2D array C++
- class not declared in scope – even though .h was included
- Best C/C++ Network Library
- How to check that an element is in a std::set?
- Best way to extract a subvector from a vector?
- C++ Fractions Class
- #pragma once vs include guards?
- Debug vs Release in CMake
- C++ Remove punctuation from String
- prototype for “….” does not match any in class “…”
- Nested For – Loops to create multiplication table C++
- The system cannot find the file specified. in Visual Studio
- How can I get and use the header file
in my C++ program? - cin.eof() functionality
- Implementing a tree in C++
- Drawing Circle with OpenGL
- Run-Time Check Failure #2 – Stack around the variable ‘foo’ was corrupted
- How to implement “Press Any Key To Exit”
- Reading and writing binary file
- GCC -fPIC option
- C++ : Exception occurred in script: basic_string::_S_construct NULL not valid
- Xcode 11.1: iostream’ file not found
- non-member function cannot have cv-qualifier
- too many initializers for ‘int [0]’ c++
- function does not take 1 arguments c++
- overloaded function with no contextual type information
- How exactly do lookup tables work and how to implement them?
- Vector declaration “expected parameter declarator”
- C++ Destructors with Vectors, Pointers,
- Why do I get: -9223372036854775808
- Return a 2d array from a function
- Linker error: “linker input file unused because linking not done”, undefined reference to a function in that file
- What is the difference among ios::app, out, and trunc in c++?
- Where can I find ‘winmm.lib’ (I’m using Visual Studio 2012)
- What is a `char*`?
- Does static constexpr variable inside a function make sense?
- “Incomplete type not allowed ” when creating std::ofstream objects