How to retrieve the user name from the user ID How to retrieve the user name from the user ID unix unix

How to retrieve the user name from the user ID


You use getpwuid to look up the password file entry for a particular UID (which includes the user name, but now not the password itself) and getgrgid to look up the group file entry for a particular GID.


check my code for username:

#include <unistd.h>#include <sys/types.h>#include <pwd.h>string getUser(uid_t uid){    struct passwd *pws;    pws = getpwuid(uid);        return pws->pw_name;}

for groupname you can use getgrgid.