Retrieving Username in C++

Discussion in 'Mac Programming' started by GenghisKhan, Jan 29, 2009.

  1. macrumors newbie


    Jan 18, 2009
    Hi there, I'm working on a small XCode project to get my C++ skills up to par again.

    I need to retrieve the username to use with a path command in "int execpl(char *path, char *arg, ...)". Since this app is going to run on somebody else's Mac and I don't know his username, I need a smart way to retrieve it.

    I could get it from the environment variable $USER but I don't know how to do this in C++.

  2. thread starter macrumors newbie


    Jan 18, 2009
    I looked it up in one of my linux books

    You can use "stdlib.h" apparantly, to use char *getenv(const char *name);

  3. Moderator emeritus


    Jun 15, 2000
    It might be better to use CSCopyUserName() from the CoreServices framework, but that's not cross-platform if you're going that route.

Share This Page