1 Directory operations Change the access rights of a directory or a file
#include <sys/stat.h>int chmod (constchar//mode like 0777
The file specified by the path parameter is modified to have access rights given by the mode parameter.
When the Modify permission successfully returns 0, the modification fails to return-1.
(Man 2 chmod) Get current directory
#include <unistd.h>
Char *getcwd (char// Gets the current directory, equivalent to the PWD command
Copies the current working directory absolute path to the memory space referred to by the parameter buf, and the parameter size is the space size of BUF. When this function is called, the memory space referred to by BUF is large enough that if the string length of the absolute path of the working directory exceeds the size of the parameter size, the value returned Null,errno is Erange.
If the parameter buf is NULL,GETCWD () automatically configures memory based on the size of the parameter size (using malloc ()), if the parameter size is also 0, then GETCWD () determines the configured memory size based on the string level of the absolute path of the working directory. The process can free up this space by automatically taking advantage of this string after it is exhausted. So the usual form: GETCWD (NULL, 0);
Gets a success that returns a pointer to the current working directory, with a general value equal to BUF.
(Man 3 getcwd) change the current directory
#include <unistd.h>int chdir (constchar// Modify current directory, that is, switch directory, equivalent to CD command
Used to change the current working directory to the directory indicated by the parameter path.
The modification returns 0 successfully, and the incident returns-1.
Creating and deleting Catalogs
#include <sys/stat.h><sys/types.h><unistd.h>
int mkdir (constchar *pathname, mode_t mode); // Create directory, mode is directory permission
int rmdir (constchar *pathname); // Delete directory
Create or delete successfully returns 0, failure returns-1.
(Man 2 mkdir, man 2 rmdir)
Get directory information
#include <sys/types.h><dirent.h>
*opendir (constchar *name); // struct dirent *readdir (DIR *dir); // A struct pointer that reads a directory of information and returns the item information int closedir (DIR *dir); // Close the catalog file
Opendir () is used to open the directory specified by the parameter name and returns the directory stream of the dir* pattern, similar to the file Operation function open (), which is followed by the use of this return value for both reading and searching of the directory. The function fails to return NULL.
The Readdir () function is used to read the directory information and returns a struct pointer that holds information about the directory. An error occurred or was read to the end of the directory file to return the null;dirent structure as follows:
On Linux, the dirent structure isDefined asfollows:structdirent {ino_t D_ino; /*inode Number*/off_t D_off; /*offset to the next dirent*/unsigned ShortD_reclen;/*length of this record*/unsignedCharD_type;/*type of file; not supported by all file system types*/Chard_name[ the];/*filename*/};
Usually we only care about the D_name information in the dirent structure.
Kris_ Sample Code _1
/************************************************************************* > File name:my_opendir.c > Author:krischou > Mail:[email protected] > Created time:tue 10:19:06 AM CST *********************** *************************************************/#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<dirent.h>#include<string.h>intMainintargcChar*argv[]) {DIR*Pdir; structDirent *Pent; Pdir= Opendir (argv[1]); if(Pdir = =NULL) {Perror ("Opendir"); Exit (1); } while((Pent = Readdir (pdir))! =NULL) {printf ("%u%u%u%x%s \ n",pent->d_ino,pent->d_off,pent->d_reclen,pent->d_type,pent->d_name); } closedir (Pdir); return 0;}
The above code reads all the files in the directory entered by the command line (information contained in dirent)