Hi everyone,

I thought since the topic of getting the end user's home directory or
user folder came up on the Audyssey list recently I thought I'd give
some game developers some code examples on how to do this in languages
like Python and C++.
That way you guys can make your code compatible with new security
features such as User Account Control on Windows, and support
multiuser environments like Linux.

For those of you using Python it seems to be the easiest and most
cross-platform independent language for saving files to the end users
home/users directory. For example, a simple script like this would get
and display the user's home directory on all platforms.

>From os.path import expanduser
directory = expanduser(“~”)
print “Your home directory is “ + directory + “.”

Unfortunately for languages like C and C++ there is no built in
function or library that is 100% cross-platform compatible. There are
OS specific functions like ShGetFolderPath() on Windows to get the
user directories, but there is a simpler and more practical function
that works most of the time without getting into OS specifics. The
cstdlib header, which ships with every C++ compiler, comes with a
getenv() function that allows you to use environment variables which
usually exists on most operating systems.

For example, on Linux we can use the getenv() function to access the
HOME environment variable to return the path to the home directory
like this.

#include <iostream>
#include <cstdlib>

int main()
{
        // Get the users home directory
        char* directory = std::getenv(“HOME”);

        // Print the user's home directory
        std::cout << “Your home directory is “ << directory << “.\n”
;

        // Exit the program
        return 0;
}

Obviously, that is all well and good for Linux operating systems, but
Windows is a different matter. There are a number of different
directories, but usually for games and other applications their saved
data goes in the Application Data directory using the APPDATA
environment variable like this.

#include <iostream>
#include <cstdlib>

int main()
{
        // Get the users application data directory
        char* directory = std::getenv(“APPDATA”);

        // Print the user's application data directory
        std::cout << “Your application data directory is “ << directory << “.\n”
;

        // Exit the program
        return 0;
}

Obviously, these two examples assumes the HOME and APPDATA environment
variables are set which they should be, but there are certainly cases
where they may not be set causing your program to fail. However, as
these environment variables are fairly constant you can generally rely
on them being there to read and write files to and from the user's
home and user directories on Windows and Linux. So hope this info
helps someone.

Cheers!

---
Gamers mailing list __ Gamers@audyssey.org
If you want to leave the list, send E-mail to gamers-unsubscr...@audyssey.org.
You can make changes or update your subscription via the web, at
http://audyssey.org/mailman/listinfo/gamers_audyssey.org.
All messages are archived and can be searched and read at
http://www.mail-archive.com/gamers@audyssey.org.
If you have any questions or concerns regarding the management of the list,
please send E-mail to gamers-ow...@audyssey.org.

Reply via email to