I am writing small c++ code to access and edit certain text file in user's home directory. Currently I have following code (this is the relevant part):
bool core(void) {
std::string autostart_entry = "";
std::string user_entry = "";
std::fstream username;
username.open("username.txt", std::fstream::in);
std::string location;
std::string user_name;
if (username.fail()) {
username.open("username.txt", std::fstream::out);
std::cout << "What's your system username? ";
std::getline(std::cin, user_name);
username << user_name;
}
else
username >> user_name;
username.close();
location = "/home/" + user_name + "/.config/openbox/autostart";
...
}
This way, as you can see, I ask user for his username, and append it to the location string, is there any easy way to find user's home directory without asking for user's input? I have tried "~/..." and it doesn't work.
I know I could scan "/etc/passwd" file to find it from there but I am wondering if there is another way.