Usage of traverse Folder---opendir under Linux

Source: Internet
Author: User

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

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: 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.