How do I make a directory/folder with c++. I’ve tried to use the mkdir() without success. I want to write a program that cin’s a variable and then uses this variable to create sub-directory’s and files with in those.
my current code. It says the + operator in the mkdir() says error no operand
char newFolder[20];
cout << "Enter name of new project without spaces:\n";
cin >> newFolder;
string files[] = {"index.php"};
string dir[] = {"/images","/includes","/includes/js","/contact","about"};
for (int i = 0; i<=5; i++){
mkdir(newFolder + dir[i]);
ofstream write ("index/index.php");
write << "<?php \n \n \n ?>";
write.close();
}
You need to
#include <string>, thestd::stringoperators are defined in that header.The result of the expression
newFolder + dir[i]is astd::string, andmkdir()takes aconst char*. Change to:Check the return value of
mkdir()to ensure success, if not usestrerror(errno)to obtain the reason for failure.This accesses beyond the end of the array
dir:there are
5elements indir, so legal indexes are from0to4. Change to:Use
std::stringfornewFolder, rather thanchar[20]:Then you have no concern over a folder of more than 19 characters (1 required for null terminator) being entered.