
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Directory or Folder with C/C++ Program
Creating a folder or directory on your computer is an important task in programming. A directory is like a container that helps store and organize files. In C and C++, you often need to create directories to store data, logs, or configuration files.
Creating directories makes file management easier. For example, if your program needs to store logs, it should check if a "logs" folder exists. If not, it can create the folder automatically, so you don't have to do it manually.
In this article, we'll show you how to create a directory in C or C++.
Creating Directories in C/C++
In this article, we'll show two ways to create directories:
Using mkdir()
In this approach, we use the mkdir() function to create a directory. It's part of the sys/stat.h library and requires two arguments: the name of the directory and the permissions (typically 0777 for full read, write, and execute access for all users).
Example
In this example, we are going to show how to use mkdir() to create a directory.
#include <iostream> #include <filesystem> int main() { // Define the path for the new directory std::filesystem::path dirPath = "logs"; // Attempt to create the directory if (std::filesystem::create_directory(dirPath)) { std::cout << "Directory created successfully." << std::endl; // Success message } else { std::cout << "Directory creation failed or already exists." << std::endl; } return 0; }
Once you run the program above, you will get the following output.
Directory created successfully.
In case the directory already exists, the mkdir() function will return an error, which is handled by the perror() function to display a message.
Time Complexity: O(1), Creating a directory is a constant-time operation.
Space Complexity O(1), Only a fixed amount of space is used to store the directory name and permissions.
Using std::filesystem
In this approach, we use the std::filesystem library, which was introduced in C++17, to handle file system operations in a cleaner and safer way, including creating directories.
Example
Here, we'll use std::filesystem::create_directory() to create a directory. The create_directory() function creates the specified directory and returns true if successful. It returns false if the directory already exists or if the operation fails.
#include <iostream> #include <filesystem> int main() { std::filesystem::path dirPath = "logs"; // Try to create the directory and check if it succeeds if (std::filesystem::create_directory(dirPath)) { std::cout << "Directory created successfully." << std::endl; } else { std::cout << "Directory creation failed or already exists." << std::endl; } return 0; }
Once you run the program above, you will get the following output.
Directory created successfully.
Time Complexity: O(1), Creating a directory is a constant-time operation.
Space Complexity: O(1), The space used is constant, as we only need to store the directory path.
Conclusion
In this article, we learned how to create directories in C and C++. We covered two methods: using the mkdir() function in C and the std::filesystem library in C++ (for C++17 and later). Both methods make it easy to create directories, with C++ offering better error handling and flexibility.