Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
Accessing linux user home directory with fstream

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.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Your best bet here is probably to use the getenv function:

#include <stdlib.h>

const char* homeDir = getenv("HOME");

The $HOME environment variable is generally always set under linux, and it will return you a string to a users home directory (even when it isn't under /home)

EDIT: This will only work for the home directory of the user running the program. If you want the home directory for a different user, you will need to use another approach

EDIT2: Actually, thinking about this for more than 1 second... the above will work, and you should use it first. However, if HOME is not set, you can use getpwuid:

#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>

const char *homedir = getenv("HOME");
if ( homedir == NULL ) {
    homedir = getpwuid(getuid())->pw_dir;
}
over 4 years ago · Santiago Trujillo Relatório

0

If you wanted to go to the home directory, just use chdir("~")

else,

This is a dirty hack but it works

#include <unistd.h>
char currdir[100];
char homedir[100];    
getcwd(currdir); //store the current directory in currdir
chdir("~"); // change the working directory to user's home directory
getcwd(homedir); // get the full address
chdir(currdir);   // go back to the previous directory
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda