How to create a directory or folder in C/C++?
A directory is to lists all files and subdirectories in a directory, set of the files will be kept in the directory, which is a location. A subdirectory is a directory that is inside the root directory and can include other subdirectories. You can quickly present different types of files and subdirectories that are present in a directory using the computer language.
Creating Directories or Folders
At present, we have various operating systems, for example let's talk about Windows and Linux, have other processes for creating directories and files. Additionally, there are several compilers that can be utilized, such as GCC/G++ compilers for Linux and Turbo C for Windows.
To create directories, the Linux operating system offers the following system call:
#include <sys/stat.h>
The data supplied by the functions fstat(), lstat(), and stat are defined by the header file "sys/stat.h" ().
#include <sys/types.h>
Typedef symbols and structures are defined in the sys/types.h header file.
Syntax: int mkdir(const char *pathname, mode_t mode);
mkdir plays a significant role in creating directories or folders in either c or c++ programming languages.
You can complete this process by utilizing the mkdir() method. This function allows for the creation of directories. A new, empty directory with the name filename is created by the mkdir() method.
The directory name is specified using the 'pathname' option.
The parameter mode specifies the permissions to utilise. The process' umask modifies it as usual: the generated directory's permissions are (mode & umask & 0777). The operating system determines the other mode bits of the newly created directory.
Create Directory/Folder with C/C++ Program
The following program will show how to create a directory/folder with C and C++. This program will create a folder in a specific directory path with the mkdir() function. The mkdir() function accepts an argument which is the name and path of the directory.
C and C++ program to create a directory/folder
Example 1
#include<iosrtream>
#include<direct.h>
using namespace std;
int main(){
//Creating a new Directory
if(mkdir("C:/MyFolder") == -1)
cerr << " Error Occured : " << strerror(errno) << endl;
else
cout << "Directory Created";
}
Output
Directory Created
There are some specific header files like #include <sys/types.h> and #include <sys/stat.h> which has to be used while creating a directory in Linux operating system:
Example 2
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include<dir.h>
void main() {
int check;
char *dirname;
clrscr();
printf("Enter a directory path and name to be created (C:/name):");
gets(dirname);
check = mkdir(dirname);
if (!check)
printf("Directory successfully created\n");
else
{
printf("Unable to create directory\n");
exit(1); }
getch();
system("dir/p");
getch();
}
Output
Enter a directory path and name to be created (C:/name): C:/Programs
Directory successfully created.
There are also different methods available to create these directories
Creating Directory by using CreateDirectory Method:
An IOUtils Method called CreateDirectory Method (System.IOUtils.TDirectory.CreateDirectory) as this method is highly helpful in organizing a new directory at the specified path. We can use the CreateDirectory function from the filesystem library to establish a new directory at the specified path. CreateDirectory tries to create the directories specified in the path if they don't already exist.
Here, let's look at a simple example to CreateDirectory() Method:
TDirectory::CreateDirectory( L"D:\\MyFolder");
Creating Directory by using create_directory std::filesystem method:
Create directory() is a method in the C++ standard that was added with C++ 17 standards. As shown in the example below, we can also utilize this function in C++ and C++ Builder apps.
Example
#include <iostream>
#include <filesystem>
int main()
{
std::filesystem::create_directory("C:\\MyFolder3");
}
Creating Directory by using with System Commands
You can use the std::system() command to use system commands like cd, mkdir, and rmdir on Windows and some other operating systems. For instance, let's consider mkdir command for it's specific usage to create a folder, the xcopy command to copy it to another folder, and the rmdir command to delete it. See illustration underneath.
Example
#include <iostream>
int main()
{
std::system("mkdir D:\\MyFolder");
}
How to access the existing directory or folder
We can get the current directory using the getcwd function.
On many Unix-based systems, a POSIX-compliant function called getcwd can be used to retrieve the current working directory. The function requires two parameters: the size of the buffer and the first char* buffer, which stores the directory path name. As an arbitrary example, observe that we are allocating a 256-element buffer and declaring a char fixed-length array. If the call is successful, reading the char buffer allows printing the current directory's stored name.
Program to access the directory in C/C++:
#include <iostream>
#include <string>
#include <filesystem>
#include <unistd.h>
using namespace std;
int main()
{
char temp[256];
getcwd(temp, 256);
cout << "Current working directory is: " << temp << endl;
return EXIT_SUCCESS;
}
Output:
Current working directory: D:/temp/programs
Advantages of creating a directory
The fundamental justification for doing this is that headers are required for compiled libraries in order for the user to consume them. By default, the headers that are available for public consumption are those found in the include directory. There may be internal headers in the source directory, but they are not intended to be shared with the generated library.
Therefore, to use the library, link to the binary and add the include directory to the header paths of your build system. Similarly, you may determine which files need to be transferred to the central location when installing your compiled library and which files don't.
Also, logical groupings of similar headers in the directory hierarchy. You don't need to know every specific of the exact locations of files; only a few directories need to be included in the includes path. Without modifying the code, libraries and the headers that go with them can be updated.
Convenient! You don't have to use the same build environment as I do, but we may frequently achieve almost identical results and reliable construction. Rearranging the paths in an includes macro or environment variable does not affect the header files that are produced during the build; portable structures Simply package everything from the build's root and send it to another developer. Information proximity. You are aware of the header's exact location.