1,105,423 Community Members

Insert string in directory path OS X mkdir

Member Avatar
Billy D
Newbie Poster
9 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 
string cmd("mkdir \"/Users/" + user + "/Library/Application Support\"");

system(cmd.c_str());
Member Avatar
Billy D
Newbie Poster
9 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

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: '\\'.

Member Avatar
Billy D
Newbie Poster
9 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 2 Years Ago by Narue
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: