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?

Dani AI

Generated

Nice, 's concatenation is the simplest fix for the original question — it’s exactly what needed to insert the username into the command string. A few follow-up notes that make the solution safer and more robust for real programs:

  • Avoid spawning a shell when you can. Calling system("mkdir ...") forces you to worry about quoting, escaping, and shell injection (if any part of the path could contain unexpected characters). On modern compilers prefer the filesystem API so you don’t need to worry about spaces or escape sequences at all.

  • Example (C++17 and later): get the home directory and create the nested folders with std::filesystem. This handles the space in "Application Support" naturally and reports errors through std::error_code.

#include <filesystem>
#include <cstdlib>
#include <system_error>

const char* home = std::getenv("HOME");
if (!home) /* handle missing HOME */;

std::filesystem::path p = home;
p /= "Library";
p /= "Application Support";

std::error_code ec;
if (!std::filesystem::create_directories(p, ec)) {
    // check ec and handle failure
}
  • Why you saw the compiler warning: in a C++ string literal a backslash begins an escape sequence; \ is not a valid escape, so the compiler warns. If you must build a shell command, either escape the backslash as \\ or use a raw string literal (R"( ... )") to avoid doubling backslashes. Better yet, don’t call the shell unless necessary.

  • Practical tips: prefer getenv("HOME") or POSIX getpwuid(getuid()) instead of calling whoami and writing a temp file; always check return/error codes; and if you must use system, sanitize inputs and quote paths carefully.

These small changes make the code clearer, safer, and portable across macOS installs where "Application Support" is a normal directory name containing a space.

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.