How Do I Make Directory Using C Program - mkdir()
Programming talent is all about making impossible things to possible by integrating different kind of aspects of programming. Perhaps we can tell that Linux Operating system is ruling the world with its compatibility.
Though, people always like to go with the basic of the programming language. Now, its about creating directory.
mkdir on console
As a programmer we use mkdir command to create directory through console. It will create a directory at the specified path. This is console specified command to make the directory. We all known about this mkdir command but one function is there to allow us to make it as possible as mkdir do on console.
mkdir() in C Program
At some point of time while programming we are supposed to get the current directory in c program. The syntax of the function that is used to create a directory is as follows:
SYNTAX
#include <sys/stat.h>
int mkdir(const char *directorypath, mode_t mode);
DESCRIPTION
directory - This is can be constant or normal character array which contains the path of a directory to be created.
mode - The permission of the directory can be initialized with the mode parameter
RETURN VALUE
Upon successful completion, mkdir() function will return 0. In case of failure reason -1 will be returned.
EXAMPLE:
For more clearance, have a look at below example.
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
int main()
{
int retval;
retval = mkdir(""/home/itachi/Prkash","S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH");
if(retval != -1)
printf("File has been created\n");
retunr 0;
}
#include<sys/types.h>
#include<sys/stat.h>
int main()
{
int retval;
retval = mkdir(""/home/itachi/Prkash","S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH");
if(retval != -1)
printf("File has been created\n");
retunr 0;
}



Comments