string currentUser()
{
    string user;
    ofstream outf("CurrentUser.dat", ios::trunc);
    system("whoami >> CurrentUser.dat");
    outf.close();
    ifstream inf("CurrentUser.dat");
    inf >> user;
    return user;
}

This is a function that gets the name of the current active user. It creates a data file, with the contents being the username. The problem is, to create a file path that is the same as all of the other files created by this program, the program needs to know what the username is. It cannot do this until this function is called... it becomes a sort of catch 22ish recursive function call situation. I want this data file grouped in the same directory as all of the other data and script files, but how? Thanks in advance

How about finding directory of argv[0]?

for a start I am very curious about your system call there. You don't need it or you don't need your outf variable. Either the one or the other is superfluous.
Since you run "whoami" probably get rid of outf.
Are you running your program from the commandline? If so your outputfile would be in the current directory, if you use an IDE like VS then you can configure in the properties where your working directory is. Otherwise set an environment variable/configuration/registry to where you want your files and make you code read it and customise your system call.

@tonyjv- I'm not sure if I know how to do what you're saying

@drkybelk- I make the system call so I can store the name of the current user in a data file, which other functions of this program can use. The other functions create AppleScript scripts in a directory (which can only be made if the program knows the current active user, as that is part of the path). To know the full directory path, I need to know the current active user.

void createSafariScript()
{
    string user = currentUser();
    string filePath = "/Users/" + user + 
    "/Library/Application\\ Support/SystemPreferences/CloseSafari.scpt";
    cout << filePath;
    ofstream outf(filePath.c_str());
    outf << "tell application \"Safari\"\n";
    outf << "\tquit\n";
    outf << "end tell";
    outf.close();
}

This should create a script in the specified directory with those contents, right?

I must explain myself in Python as I do not know well C++ string operations, this is how I get user directory name in WindowsXP if Python file is under it and run (maybe I should stay in Python forum, but I like to make fool of myself here, to learn more ;) At least I am not giving away ready solutions)

import sys, os

print sys.argv[0]
userparent = os.altsep + 'Documents and Settings' + os.altsep
username = sys.argv[0].split(userparent)[1].split(os.altsep)[0]
print username
""" Output:
C:/Documents and Settings/Veijalainen.YKSI/Omat tiedostot/userdir.py
Veijalainen.YKSI

"""

would that code you just posted compile on OS X?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.