mkdir function and RmDir function _c language in C language under Linux

Source: Internet
Author: User
Tags mkdir parent directory

The mkdir function is used to create a directory. The format is as follows:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int mkdir (const char *pathname,mode_t mode);

Where the parameter pathname is the directory name of the newly created directory, mode specifies the access rights for the directory that will be modified by the file creation Mask (umask).

This function creates an empty directory named Pathname, which automatically contains the "." and ".." 2 registered entries. The user ID of this newly created directory is set to the valid user ID of the calling process, with the group ID of the parent directory or the valid group ID of the process.

If the call succeeds, mkdir updates the St_atime, St_ctime, and St_mtime of the directory, updates the St_ctime and st_mtime of its parent directory, and then returns 0. If the call fails, MKDIR returns-1.

The parent directory of the new directory specified by pathname must exist, and the calling process must have write access to the parent directory and search permissions for each of the pathname involved in the path directory.

The Rndir function deletes an empty directory, which is formatted as follows:
#include <unistd.h>
int rmdir (const char *pathname);
When using the RmDir function, the directory must be empty or the call fails, and the function returns-1. When successful, the function returns 0.
Example: Create a new directory, and then delete the directory.

Copy Code code as follows:

#include <sys/types.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
int main (int argc,char *argv[])
{
Char path[1000];
Char file[1000];
/*
if (argc!=2)
{
printf ("Usage mk<pathname>\n");
return 1;
}
*/
ARGV[1] = "Test";
GETWD (path); Get Current working directory
printf ("Current dirctory is:%s\n", path);
if (mkdir (argv[1],s_irwxu| s_irgrp| s_ixgrp| S_iroth) <0)//Create a new directory
{
printf ("mkdir failed\n");
return 2;
}
if (ChDir (argv[1]) <0)//change current working directory to new directory
{
printf ("ChDir failed \ n");
return 3;
}
GETWD (path);
printf ("mkdir successed.\n New current directory is:%s\n", path);
RmDir (path); Delete new Directory
printf ("%s is removed\n", path);
return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.