Create and Delete Folder in C++ (2024)

Generally, we just use a right click to create or delete a folder in our windows OS! But there are other ways also to do the same task. One of the very common way is to use command prompt (i.e., cmd).

So first we will see how we create a folder using cmd. For this open a command prompt, navigate to the folder where you want to create a new folder using cd. Then use command mkdir followed by the name of the folder you want to create. After that you can simply use command dir to check if the folder has been created.

Now we will see how to create a folder using C++:

To accomplish this task we are going to use mkdir() function. mkdir() function creates an empty folder with the specified path name.

Function format: int mkdir(const char* pathname)

mkdir() function works little bit differently on POSIX(Linux, Unix, etc). It takes two parameter where first parameter is pathname and second is mode.
int mkdir(const char *pathname, mode_t mode);

Here mode is a bitwise-OR field that specifies what permissions (read,write or both) the directory has when it is created.

Also while working in Unix we have to include <unistd.h> instead of <direct.h>

Following program shows how to create folder in C++:

#include<iosrtream>#include<direct.h>using namespace std; int main(){ //Creating File if(mkdir("E:/MyFolder") == -1) cerr << " Error : " << strerror(errno) << endl; else cout << "File Created"; }

Explanation for above code:

iostream - It provides basic input and output services for C++ programs.

<direct.h> - <direct.h> is a C/C++ header file provided by Microsoft Windows OS which contains functions for manipulating files/folders. mkdir() is one of the functions under this file.

mkdir() - It takes path of the folder as argument and creates a folder.
Return Type of mkdir(): After successfully creating a folder mkdir() returns a 0. Otherwise it returns -1, no folder is created and error is disppayed.

After successully creating a folder it displays following output:
Create and Delete Folder in C++ (1)

In above code we can see "errno" which displays error message when one of the following errors occur.
1) [EACCES] -This error occurs a) If parent directory in which we are creating new file does not allow "write" permission. b) If one of the directories in the path name does not allow search permission.
2) [EEXIST] - This error is diplayed if file which we are trying to create with specified name is already present.
3) [ELOOP] - This type of error occurs if number of symbolic links in the pathname specified is more than POSIX_SYMLOOP (This is specified in limit.h header file). Symbolic Link: It is not an actual file, it is an entry to a file system that points to the file
4) [EMLINK] - If the link count of the directory in which we are creating new file exceeds LINK_MAX then EMLINK error occurs. The value of LINK_MAX can be determined using the pathconf() or the fpathconf() function. Link cout: It is the number of hard links to that file, it is always atleast two and increases with each subfile in the particular file.
5) [ENAMETOOLONG] - If the pathname (argument specified inside the mkdir() function) is longer than is longer than PATH_MAX characters or if some component of the name is longer than NAME_MAX characters then ENAMETOOLONG error occurs. The values of PATH_MAX and NAME_MAX values can be determined using pathconf() function.
6) [ENOENT] - This error occurs ifthe pathname specified inside mkdir() function or one of the components in path name does not exist.
These are some of the situations where mkdir() fails to create a folder and returns -1.

One of the situations where mkdir fails( [EEXIST] - The named file exist)
Create and Delete Folder in C++ (2)

So first we will see how we delete a folder using cmd. For this open a command prompt, navigate to the folder where the folder you want to delete is located using cd. Then use command rmdir followed by the name of the folder you want to delete. After that you can simply use command dir to check if the folder has been deleted.

Now we will see how to delete a folder using C++:

To accomplish this task we are going to use rmdir() function. rmdir() function deletes a folder with the specified path name.

Function format : int rmdir(const char *path);

Following program shows how to delete a folder using C++:

#include<iosrtream>#include<direct.h>using namespace std; int main(){ //Creating File if(mkdir("E:/MyFolder") == -1) cerr << " Error : " << strerror(errno) << endl; else cout << "File Created"; }

Explanation for above code:
iostream and <direct.h> has already been explained above.

rmdir() - It take path of the folder as argument and delets the specified folder.
Return Type of rmdir() : After successfully deleting a folder rmdir() returns 0. Otherwise it returns -1, folder is not deleted and error is displayed.

After successfully deleting a folder it displays following output:
Create and Delete Folder in C++ (3)

In above code we can see "errno" which displays error message when one of the following errors occur.
1) [EACCES] - This error occur a) if the directory in the pathname (argument specifies in rmdir() function) does not allow search permission or b) if one of the directory in the pathname does not allow search permission.
2) [EBUSY] - If the file we are trying to delete is in the process or used by some other process which is running at the same time then the EBUSY error occurs.
3) [ENOTEMPTY] - This error occurs if we are trying to delete a file which is not empty (we can only delete a file which is empty i.e. it should not contain any other objects) or if there are hard links to the directory other than dot or a single entry in dot-dot.
Create and Delete Folder in C++ (4)
5) [EINVAL] - This error occurs if the arguments passed in rmdir function are incorrect or if the operations specified the particular object is not supported by the object.
6) [EIO] - A physical I/O error has occurred or if a referenced object was damaged.
7) [ENAMETOOLONG] - If the pathname (argument specified inside the rmdir() function) is longer than is longer than PATH_MAX characters or if some component of the name is longer than NAME_MAX characters then ENAMETOOLONG error occurs. The values of PATH_MAX and NAME_MAX values can be determined using pathconf() function.
8) [ENOENT] - This error occurs ifthe pathname specified inside rmdir() function or one of the components in path name does not exist.
9) [ENOTDIR] - If the pathname specified inside a rmdir() function exist but it is not a directory when it is expected to be a directory.
These are some of the situations where rmdir() fails to delete the folder and returns -1.
example:
Create and Delete Folder in C++ (5)

So today we have seen how to use mkdir() and rmdir() functions to create and delete folder in C++.

NOTE : Above codes will not run on online IDE, you have to run them on your local IDEs.

Create and Delete Folder in C++ (2024)

FAQs

How to create a new folder in C++? ›

Here's an example using <filesystem> :
  1. cpp.
  2. #include <iostream>
  3. #include <filesystem>
  4. int main() {
  5. std::filesystem::path folderPath = "path/to/your/folder";
  6. try {
  7. std::filesystem::create_directory(folderPath);
  8. std::cout << "Folder created successfully." << std::endl;
Feb 7, 2023

How to delete a directory using C++? ›

Solution. On most platforms, you will be able to use the rmdir system call that is shipped with most compilers as part of the C headers. There is no standard C++, portable way to remove a directory. rmdir takes on different forms in different OSs, but regardless, you can use it to remove a directory.

How delete [] is different from delete in C++? ›

Failure to comply will cause undefined behaviour. The reason why there are separate delete and delete[] operators is that delete calls one destructor whereas delete[] needs to look up the size of the array and call that many destructors. Naturally, using one where the other is required can cause problems.

How you will delete a file or folder explain the steps? ›

Delete a file or folder (Windows 10)
  1. Click File Explorer icon.
  2. Go to the location where stores your file or folder.
  3. Click the name of the file or folder you wish to delete.
  4. Press the delete key (on the keyboard) or right click the file or folder and click Delete.

How to create a folder in C code? ›

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.

How do I create a new folder? ›

To make a new folder, simply open File Explorer and select Home in the upper-left corner of the window to find the New folder icon. A new folder will automatically appear on the screen, and you can rename the folder by right-clicking on the name.

How do I delete a folder in C? ›

rmdir() removes the directory even if it is the working directory of a process. If rmdir() is successful, the change and modification times for the parent directory are updated.

How do I delete a file in C++? ›

remove() -- Delete File

The remove() function deletes the file specified by filename . If the filename contains the member name, the member is removed or the file is deleted. Note: You cannot remove a nonexistent file or a file that is open.

What is delete command in C++? ›

delete is an operator that is used to destroy array and non-array(pointer) objects which are dynamically created by the new operator.
  • delete can be used by either using the delete operator or delete [ ] operator.
  • The new operator is used for dynamic memory allocation which stores variables on heap memory.
Sep 8, 2023

What does remove () do in C++? ›

How to use the remove() function in C++ The remove() function in C++ helps to remove all the occurrences of a value within a specified range. This function is available in the <algorithm. h> header file.

What is the new and delete operator in C++? ›

New and Delete Operators in C++ The new operator is used to dynamically allocate memory on the heap for an object or an array of objects. And delete operator is used to deallocate the memory. This article focuses on two very important operators :New and delete operators in C++.

When to use free or delete in C++? ›

delete and free() in C++

In C++, the delete operator should only be used for deallocating the memory allocated either using the new operator or for a NULL pointer, and free() should only be used for deallocating the memory allocated either using malloc(), calloc(), realloc() or for a NULL pointer.

How to create a folder and rename it? ›

You might need to click Browse or Computer, and navigate to the location for your new folder. In the Save As dialog box that opens, click New Folder. Type the name of your new folder, and press Enter. Note: You can't use slashes, colons, semicolons, dashes, or periods in your folder name.

Which command is used to delete folder? ›

Use the rmdir command to remove the directory, specified by the Directory parameter, from the system. The directory must be empty (it can contain only . and ..) before you can remove it, and you must have write permission in its parent directory.

What is the mkdir command in C++? ›

The mkdir() function shall create a new directory with name path. The file permission bits of the new directory shall be initialized from mode. These file permission bits of the mode argument shall be modified by the process' file creation mask.

How do I create a new folder in visual code? ›

VS Code - How to create a folder on Visual studio code

The first step is to open Visual studio code, and Click Documents dropdown. Click Folder icon. Type in Folder's name, and Press Enter in your keyboard. That's it.

How to create file in C++? ›

To create a C++ file:
  1. In the Project Explorer view, right-click the HelloWorld project folder, and select New > Source File.
  2. In the Source file: field, type main. cpp. ...
  3. Click Finish.
  4. A Comment template probably appears at the top of an otherwise empty file. ...
  5. Click File > Save.

How do you create a new library in C++? ›

Creating Static Library in C++
  1. Create Header and Library Source Code Files.
  2. Compiling the Library Source Code to Object File.
  3. Compile the Main Program Using the Static Library.
  4. Execute the Program.
Mar 18, 2024

References

Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 6265

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.