Finding and using the current directory
There's no doubt that after programming for a while, the inconveniences of reading or writing to files start to quickly build up. It may not be so bad when running programs outside of your compiler, but using a relative path can be a pain while debugging, because it is no longer relative to the directory where the executable is, but instead where the .obj
files are located. For the rest of this book, we will be using a function to obtain the full path to your executable, regardless of where it is or how it's being launched. Let's take a look at a new header file that will contain this function, called Utilities.h
:
#pragma once #define RUNNING_WINDOWS #include <iostream> #include <string> #include <algorithm> namespace Utils{ #ifdef RUNNING_WINDOWS #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <Shlwapi.h> inline std::string GetWorkingDirectory(){ HMODULE hModule = GetModuleHandle(nullptr); ...