Opendir function
Header files: #include <sys/types.h> #include <dirent.h>
Function: DIR *opendir (const char *name);
Meaning: Opendir () is used to open the directory specified by the parameter name, and returns the directory stream of the dir* pattern, similar to open (), which is followed by the use of this return value for reading and searching the directory.
About dir: detail: http://blog.csdn.net/u012349696/article/details/50083787
First talk about the DIR structure, the following is the definition of the DIR structure:
1 struct__dirstream2 { 3 void*__fd; 4 Char*__data; 5 int__entry_data; 6 Char*__ptr; 7 int__entry_ptr; 8 size_t __allocation; 9 size_t __size; Ten __libc_lock_define (, __lock) One }; A -typedefstruct__dirstream DIR;
The dir struct is similar to file and is an internal structure, and the following functions use this internal structure to hold information about the directory that is currently being read (excerpt from advanced Programming in the UNIX Environment (second edition)). The function dir *opendir (const char *pathname), which is the open file directory, returns a pointer to the DIR struct, which is used by the following functions:
1 struct dirent *readdir (DIR *dp); 2 3 void Rewinddir (DIR *dp); 4 5 int closedir (DIR *dp); 6 7 long telldir (DIR *dp); 8 9 void Seekdir (DIR *dp,long Loc);
With regard to the DIR structure, we know so much that we don't have to go into the structural members.
.....
Readdir function
Header files: #include <sys/types.h> #include <dirent.h>
function: struct dirent *readdir (DIR *dir);
Meaning: Readdir () returns the next directory entry point for the Dir directory stream.
struct dirent
{
ino_t D_ino; D_ino the inode for this directory entry point
ff_t D_off; D_off directory file at the beginning of this directory entry point displacement
signed short int d_reclen; D_reclen _name length, contains no null characters
unsigned char d_type; D_type D_name refers to the file type D_name filename
Har d_name[256];
};
Closedir function
Header files: #include <sys/types.h> #include <dirent.h>
function; Closedir (DIR *dir);
Meaning: Close dir stream;
example, scan a JPG file under the directory:
1STD::VECTOR<STD::string>filelist;2DIR * dirp=Opendir (ImagePath);3 structDirent *directory;4 if(DIRP)5 {6 while((Directory = Readdir (DIRP))! =NULL)7 {8 if(Strlen (Directory->d_name) <=4)9 Continue;Ten One Charbuf[5]; Amemcpy (buf,& (Directory->d_name[strlen (directory->d_name)-4]),4); -buf[4]=' /'; - the if(strcmp (BUF,". jpg")==0) - { -printf"%s\n", directory->d_name); -Filelist.push_back (directory->d_name); + } - } + A Closedir (DIRP); at } - Else -printf"cannot open Directory%s!", ImagePath);
Turn from: 50084195
The connotation and usage of Linux c--opendir functions and Readdir functions (RPM)