Determine user home path

Hello ofCommunity,
is there an easy way to determine a user’s home path on windows.

I needed default locations of music, images and videos on osx. I thought it might be useful to have a simple addon for this.


1 Like

Does oF support ~ (home directory) on unix systems?
I tried the following on osx.

new ofImage("~/Pictures/testImage.jpg"); 
//[ error ] ofImage: loadImage(): couldn't load image from " ~/Pictures/testImage.jpg " 

new ofImage("/Users/thomasgeissl/Pictures/testImage.jpg"); 
// no error


Usually what I do is using the ofSystem() function.
If I am not mistaken, doing something like this:

string path;
path = ofSystem(“echo ~”);

Thanks, I will include this in ofxStandardPaths.
I thought ~ would work.
Is there an equivalent to do this on windows?

Hi Thomas,

Here’s a snippet that should be portable across Linux/OS X/Windows

I just put it together quickly from a few references and tested it under OS X, but not Windows.

#ifdef _WIN32
#include <windows.h>
#include <shlobj.h>
#elif defined(__unix__) || defined(__APPLE__)
#include <pwd.h>
#include <unistd.h>

#include <string>

std::string getUserHomeDir()
    std::string homeDir = "";

#ifdef _WIN32
    char szPath[ MAX_PATH ];

    if ( S_OK == SHGetFolderPathA( NULL, CSIDL_PROFILE, NULL, 0, szPath ) )
        homeDir = path;
#elif defined(__unix__) || defined(__APPLE__)
    uid_t uid = getuid();
    struct passwd* pwd = getpwuid( uid );

    if ( NULL != pwd )
        homeDir = pwd->pw_dir;

    return homeDir;

int main(void)
    std::string homeDirectory = getUserHomeDir();
    printf( "User's home directory: %s\n", homeDirectory.c_str() );

    return 0;
1 Like

You could take a look at the Poco code for finding the home directory. Poco is no longer used in the core for 0.9.0+, but still supported. They recently added support for the XDG base directory specification. Here is the merge:

Also, you can use ofFilePath::getUserHomeDir.

Thanks for your answers.
I updated my code and will work on this once I have little more time.

And that would be the easiest! Not sure how I missed that :wink:

1 Like