The mkdir() rmdir() remove() function of linux programming

mkdir()

The mkdir function is used to create directories. The format is as follows:

      #include<sys/types.h>

      #include<sys/stat.h>

      #include<unistd.h>

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

    The parameter pathname is the directory name of the newly created directory, and mode specifies the access permissions of the directory. These bits will be modified by the file creation method mask (umask).

 This function creates an empty directory named pathname, which automatically contains 2 entries of "." and "..". The user ID of this newly created directory is set to the effective user ID of the calling process, and its group is either the parent directory's group ID or the effective group ID of the process.

    If the call is successful, mkdir will update the st_atime, st_ctime and st_mtime of the directory, and also update the st_ctime and st_mtime of its parent directory, and then return 0. If the call fails, mkdir will return -1.

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


The rmdir() function deletes an empty directory, which has the following format:

    #include<unistd.h>

    int rmdir(const char *pathname);

    When using the rmdir function, the directory must be empty , otherwise the call fails and the function returns -1. When successful, the function returns 0.


The remove() function is used to delete the specified file, and its prototype is as follows:

Header file: #include <stdio.h>
    int remove(char * filename);

[Parameter] filename is the name of the file to be deleted, which can be a directory. If the parameter filename is a file, call unlink() for processing; if the parameter filename is a directory, call rmdir() for processing.

[Return value] Returns 0 if successful, -1 if failed, and the cause of the error is stored in errno.

error code:

  1. The file to be written by EROFS is a read-only file.
  2. The EFAULT parameter filename pointer exceeds the accessible memory space.
  3. ENAMETOOLONG The parameter filename is too long.
  4. ENOMEM Insufficient core memory.
  5. The ELOOP parameter filename has too many symbolic links.
  6. EIO I/O access error.

If the file is in use, the actual deletion of the file is performed after you dereference it, but no user intervention is required, and the file will be deleted.
If you want to see the file deleted immediately after calling remove, it is recommended that you judge whether it is occupied. Or use system("rm -rf filename").

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813144&siteId=291194637