The first thing to say is definitely the header file, #include <sys/types.h> #include <dirent.h>
The following functions are required to traverse a folder under Linux, three of which are required, and several others are optional.
dir* opendir (const char * name); Failed to return null. Returns the DIR struct successfully. Note that dir is not in front of the struct, and if the compiler warning
struct dirent *readdir (struct dir* DIR); Failed to return null.
void Closedir (dir* DIR); Closes the Open Directory descriptor dir, releasing dir space at the same time.
The optional functions are:
Long int telldir (dir* DIR); The position of the current pointer, combined with ftell understanding, may be better understood, except that the Ftell offset is in bytes, and the unit offset here is the dir struct.
void Seekdir (dir* DIR, long int pos); Point the pointer to the offset of the POS, in conjunction with fseek understanding, except that the offset here is relative to the header (equivalent to the default Seek_set).
void Rewinddir (dir* DIR); As the name implies, the pointer from the new position to the first bit. Combined with rewind understanding. The effect is similar.
Of course there are scandir, not to see usage. So don't introduce.
The above functions are explained in the following example:
Examples of Linux programming:
The results of the operation are as follows:
Next Seekdir,telldir and Rewinddir.
These three functions do not really have a way of associating with specific scenarios, because if you do not read the Readdir reading order, the behavior of Seekdir and Rewinddir is somewhat out of control. Though knowing that he had stopped at the first few offsets, But may not know the location of the file. But I dare not say that these three functions are useless. It is just that I have not used it, if the students have used such a specific scene, can also tell me. Thank you very much.
About the directory is written here, perhaps a lot of students have begun to rewrite LS the impulse of it. You can try it as a little hobby.
Usage of traverse Folder---opendir under Linux