How to make a folder using C++?

Folders are very crucial for managing and arranging our data and files. It makes searching data when needed very easy and prevents the hassle when you need your files.

Also, not all the data on our computer are needed for doing a specific task. Taking a simple example of playing a game. If you wish to play a game on your computer, it would be convenient to start your game if you have all your games stored in a single directory, rather than searching for your game among all the files in your computer stored in a single directory.

Now, in this post, we are going to see how to make a folder using the C++ programming language.

Starting with your code

The program for creating a directory in C++ is very simple.

The IDE used for compiling the program is TurboC.



The program will focus to make a folder in the \bin directory of the application, that is the default location used by the function to make a directory.

 

#include<direct.h>

 

Only a single header file <direct.h> for this program. This header consists of many functions like creating, removing, editing directories. The name <direct.h> of the header files stands for the word directory!

 

void main()
{
  mkdir("NewFolder");
}

 

In the above code, main() function is initialized. In the main function, the function mkdir() is used. the meaning of mkdir is ‘make directory’. Using mkdir() function,a new folder is created of the name mentioned in the parenthesis. In the example mentioned above, the name mentioned in the parenthesis is ‘NewFolder’.

Also, read:

 

So a folder of name ‘NewFolder’ will be created. Since a path is not mentioned in the parenthesis and the name of the folder is only written, the folder will be created at the default location. For TurboC IDE, the default location is \bin of the same application folder.

If you want to create a folder at a different location, you can type in the exact path of the directory or folder in which you want to add a new directory.

In this way, we can create a new folder in C++ in a bit more interesting way.

Leave a Reply

Your email address will not be published. Required fields are marked *