C/C++ - Locating a user's home directory - SOLVED
David Fletcher
dave at thefletchers.net
Sun Jan 30 13:39:05 UTC 2011
// For the benefit of anybody else who searches Google
// to try to find an answer to this question:-
// Short example
// how to write a C/C++ program for Linux to determine
// find the home directory of the user who is running it
// Use the command
// g++ -Wall HomeDirTest.cpp -o HomeDirTest
// to compile this source
// Compiled and tested by running as a cron job on an
// Ubuntu 10.04 server with the executable file copied
// to /usr/local/bin/
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <fstream>
using namespace std;
int main(void)
{
ofstream DirTest;
int myuid;
struct passwd *mypasswd;
char TestFileName[30];
myuid = getuid();
mypasswd = getpwuid(myuid);
strcpy(TestFileName, mypasswd->pw_dir);
strcat(TestFileName, "/DirTestOutput");
DirTest.open(TestFileName);
DirTest << "This is a test\n\n";
DirTest << "My uid is " << myuid << "\n\n";
DirTest.close();
}
More information about the ubuntu-users
mailing list