Q:

C program to create a directory using mkdir() function

0

C program to create a directory using mkdir() function

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Input the name (path) of the directory, and then we will create the given directory using the mkdir() function.

The mkdir() function is library function of <sys/stat.h> header file which is used to create a new directory with a new path.

Syntax:

int mkdir(const char *path, mode_t mode);

The argument path defines the path of the directory, and the mode specifies the file permissions for the new directory file.

The function returns 0 if the directory created successfully, or -1 on failure.

Program:

The source code to create a directory using the mkdir() 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 mkdir() function

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main()
{
    char dirName[16];
    int ret = 0;

    printf("Enter directory name: ");
    scanf("%s", dirName);

    ret = mkdir(dirName, 0755);

    if (ret == 0)
        printf("Directory created successfully\n");
    else
        printf("Unable to create directory %s\n", dirName);

    return 0;
}

Output:

Enter directory name: temp
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 the mkdir() 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

total answers (1)

File Handling Examples Programs in C language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to create a directory using system() fun... >>
<< C program to check a specified file has read, writ...