Open In App

Create Directory or Folder with C/C++ Program

Problem: Write a C/C++ program to create a folder in a specific directory path. This task can be accomplished by using the mkdir() function. Directories are created with this function. (There is also a shell command mkdir which does the same thing). The mkdir() function creates a new, empty directory with name filename.

// mkdir() function
int mkdir (char *filename)

Note: A return value of 0 indicates successful completion, and -1 indicates failure.





Directory created.
a.out  geeksforgeeks  main.c 




// C++ program to create a directory in Linux
#include <bits/stdc++.h>
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
using namespace std;
 
int main()
 
{
 
    // Creating a directory
    if (mkdir("geeksforgeeks", 0777) == -1)
        cerr << "Error :  " << strerror(errno) << endl;
 
    else
        cout << "Directory created";
}

Directory created.

Note: Above source codes would not run on online IDEs as the program requires the directory path in the system itself.

Article Tags :