I'm not sure if this is possible. Basically, I have a function that will detect the current active user.

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

I have another function which will create a directory in the user's computer. The path will be:
/Users/*ActiveUser*/Library/Application Support.

Now, I have a data file containing only the name of the current active user. How can I insert that into a system command such as:

system("mkdir \"/Users/ActiveUserGoesHere/Library/Application Support\"");

If I read the contents of the data file into a string or other variable, can I insert them into that system command so it uses the string as part of the path?

Recommended Answers

All 4 Replies

string cmd("mkdir \"/Users/" + user + "/Library/Application Support\"");

system(cmd.c_str());

Worked beautifully; I cannot express my gratitude. One other thing, though. Why do I get a warning for:

string path = "mkdir \"/Users/" + user + "/Library/Application\ Support/System\ Preferences\"";

IDE says: "Lexical or Preprocessor Issue; Unknown escape sequence '\ '"

The code compiles/works fine, but I'm curious as to why there is a warning here

Anything preceded by a backslash is treated as an escape character. If that character isn't one of the ones the compiler recognizes, that's a Bad Thing(TM). In your case you have '\ ', which isn't recognized. If you want a backslash in the string literal, double it up: '\\'.

Goodness, you're pretty much a genius. Thank you so, so much.

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.