Getting home directory in Mac OS X using C language

后端 未结 3 852
無奈伤痛
無奈伤痛 2020-12-18 00:05

How can I get the path of home directory in Mac OS X using C language in XCode editor.

3条回答
  •  不知归路
    2020-12-18 00:17

    This should work under Linux, Unix and OS X, for Windows you need to make a slight modification.

    #include 
    #include     
    #include 
    #include 
    
    int main(void)
    {
        const char *homeDir = getenv("HOME");
    
        if !homeDir {
            struct passwd* pwd = getpwuid(getuid());
            if (pwd)
               homeDir = pwd->pw_dir;
        }
        printf("Home directory is %s\n", homeDir);
        return 0;
    }
    

提交回复
热议问题