Search code examples
c++filesystem

Open a file containing a variable name


I have the following code:

string name;
getline(cin,name);
ofstream foldercreator(name + "folder.bat");
foldercreator << "if not exist \"" << name << "\" mkdir " << name << endl;
foldercreator << "exit";

The problem is, that

string batname = "start " + name + "folder";
system(batname);
system("start " + name + "folder");

doesn't work either.

The only problem is, that it cannot open the file.


Solution

  • You can use std::string::c_str() to obtain a pointer to C string from std::string.

    string batname = "start " + name + "folder";
    system(batname.c_str());
    
    system(("start " + name + "folder").c_str());