Input the name (path) of the directory, and then create the directory using the system() function.
The system() function is a library function of <stdlib.h> or <cstdlib> header file which is used to execute the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed.
Syntax:
int system(const char *string);
The argument string is the command to be executed.
To create the directory at given path, we create the command and then pass it into system() function.
Program:
The source code to create a directory using system() function is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
// C program to create a directory
// using system() function
#include <stdio.h>
#include <stdlib.h>
int main()
{
char dirName[16];
char cmd[32] = { 0 };
int ret = 0;
printf("Enter directory name: ");
scanf("%s", dirName);
sprintf(cmd, "mkdir %s", dirName);
ret = system(cmd);
if (ret == 0)
printf("Directory created successfully\n");
else
printf("Unable to create directory %s\n", dirName);
return 0;
}
Output:
Enter directory name: newDir
Directory created successfully
Explanation:
Here, we created a character array dirName. Then we read the name of the directory from the user. Then we created the given directory using system() function and printed the appropriate message on the console screen.
Input the name (path) of the directory, and then create the directory using the system() function.
The system() function is a library function of <stdlib.h> or <cstdlib> header file which is used to execute the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed.
Syntax:
The argument string is the command to be executed.
To create the directory at given path, we create the command and then pass it into system() function.
Program:
The source code to create a directory using system() function is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
Output:
Explanation:
Here, we created a character array dirName. Then we read the name of the directory from the user. Then we created the given directory using system() function and printed the appropriate message on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer