Create Directory or Folder with C/C++ Program - GeeksforGeeks

31 May, 2020
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.
Program to create a directory in Windows using Turbo C compiler:// C program to create a folder#include <conio.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>  void main(){    int check;    char* dirname = "geeskforgeeks";    clrscr();      check = mkdir(dirname,0777);      // check if directory is created or not    if (!check)        printf("Directory created\n");    else {        printf("Unable to create directory\n");        exit(1);    }      getch();      system("dir");    getch();}Output:Directory created.
a.out  geeskforgeeks  main.c 

// C program to create a folder#include <conio.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>  void main(){    int check;    char* dirname = "geeskforgeeks";    clrscr();      check = mkdir(dirname,0777);      // check if directory is created or not    if (!check)        printf("Directory created\n");    else {        printf("Unable to create directory\n");        exit(1);    }      getch();      system("dir");    getch();}
Output:
Directory created.
a.out  geeskforgeeks  main.c 

Program to create a directory in Linux/Unix using GCC/G++ compiler:// 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";}Output:Directory created.


// 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";}
Output:
Directory created.


Note: Above source codes would not run on online IDEs as the program requires the directory path in the system itself.
This article is contributed by Rishav Raj. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
santoshbommanalli
C Programs
C++ Programs
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C Program to read contents of Whole File
How to Append a Character to a String in C
C program to sort an array in ascending order
time() function in C
C Program to Swap two Numbers
C++ Program for QuickSort
Sorting a Map by value in C++ STL
Shallow Copy and Deep Copy in C++
C++ program for hashing with chaining
delete keyword in C++