Skip to content

Instantly share code, notes, and snippets.

@sidewinder040
Last active July 5, 2025 08:19
Show Gist options
  • Save sidewinder040/4dd96b763e5aa2bd17d37b51f86dfe76 to your computer and use it in GitHub Desktop.
Save sidewinder040/4dd96b763e5aa2bd17d37b51f86dfe76 to your computer and use it in GitHub Desktop.
C++17 - Get Current Working Directory Path
#include <cstdlib>
#include <iostream>
#include <string>
std::string getHomeDirectory() {
const char* home = std::getenv("HOME");
if (home == nullptr) {
throw std::runtime_error("HOME environment variable not set");
}
return std::string(home);
}
int main() {
std::string configPath = getHomeDirectory() + "/.myapp/config.conf";
std::cout << "Config Directory: "
<< configPath
<< std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment