string currentUser()
    string user;
    ofstream outf("CurrentUser.dat", ios::trunc);
    system("whoami >> CurrentUser.dat");
    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

5 Years
Discussion Span
Last Post by pyTony

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";

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

Edited by Billy D: n/a


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


Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.