Reputation: 67
I need to make a function that creates a directory in a subdirectory of the current location. Here is what I've tried:
#include <iostream>
#include <cstring>
#include <dir.h>
#include <stdlib.h>
using namespace std;
void create(){
char nume[50];
int directory1,directory2;
directory1=mkdir("folder1");
directory2=mkdir("/folder1/ name1");
}
int main()
{
create();
}
When I run this code, the "folder1" directory does get created, but "name1" doesn't. What am I doing wrong? I also tried doing this:
#include <iostream>
#include <cstring>
#include <dir.h>
#include <stdlib.h>
using namespace std;
void create(){
char nume[50];
system("mkdir folder1");
system("mkdir /folder1/name1");
}
int main()
{
create();
}
But I get a "The syntax of the command is incorrect" message.
Upvotes: 2
Views: 1648
Reputation: 117876
I would suggest using the <filesystem>
library which is available as of C++17, in this case using std::filesystem::create_directories
#include <filesystem>
void create()
{
std::filesystem::path subfolder = "/folder1/name1";
std::filesystem::create_directories(subfolder);
}
int main()
{
create();
}
Note that create_directory
assumes all parent directories exist, otherwise create_directories
will create intermediate directories if missing.
Upvotes: 4