Home > Software engineering >  How can I create a folder in C that is named using a string or char?
How can I create a folder in C that is named using a string or char?

Time:12-01

I'm trying to create a folder with a custom name for each user that will logged in but it doesn't work. Can you please help me? I'm a beginner and it's quite difficult.

#include <iostream>
#include <direct.h>
using namespace std;

int main() {
    string user = "alex";
    _mkdir("D:\\Programe\\VS\\ATM\\Fisiere\\"   user);
    return 0;
}

I was trying to make the folder in the same way I make the files, but it doesn't work.

CodePudding user response:

_mkdir is an older function which takes a C string as it's parameter. So you have to convert the std::string that you have into a C string. You can do that with the c_str method. Like this

_mkdir(("D:\\Programe\\VS\\ATM\\Fisiere\\"   user).c_str());

This code creates a std::string by appending the path with the user string and then calls c_str on that string and then passes the result of that to _mkdir.

  • Related