In linux, traversal of folders --- opendir and other usage, in linux --- opendir

Source: Internet
Author: User

In linux, traversal of folders --- opendir and other usage, in linux --- opendir

First, it must be the header file. # include <sys/types. h> # include <dirent. h>

In linux, you need to use the following functions to traverse folders. Three of them are required, and the others are optional.

DIR * opendir (const char * name); NULL is returned if a failure occurs. DIR structure is returned if a successful result is returned. Note that no struct exists before DIR. If a compiler is added, the system will send a warning message.

Struct dirent * readdir (struct DIR * dir); returns NULL if a failure occurs.

Void closedir (DIR * dir); close the opened directory descriptor dir and release the dir space.

Optional functions:

Long int telldir (DIR * dir); the position of the current pointer, which can be better understood in combination with ftell, except that the unit of ftell offset is byte, and the unit of offset here is the DIR structure.

Void seekdir (DIR * dir, long int pos); point the pointer to the offset corresponding to the pos. In combination with fseek, the difference is that the offset here is only relative to the header (equivalent to the default SEEK_SET ).

Void rewinddir (DIR * dir); as the name suggests, the pointer is positioned to the first place. The function is similar to that of rewind.

Of course, there is scandir, which does not look at usage. So we will not introduce it.

The following example explains the above functions:

Linux Program Design Example:


The running result is as follows:



Next let's talk about seekdir, telldir and rewinddir.

These three functions cannot be associated with specific application scenarios, because the behavior of seekdir and rewinddir is a bit uncontrollable if you have not read the read sequence of readdir. although you know that you have stopped at the offset, you may not know the file at that position. but I dare not say these three functions are useless. I have never used it. If you have used such a specific scenario, you can also tell me. I am very grateful.

The directory is written here. Maybe many of you have begun to have the impulse to rewrite ls. You can try it as a small interest.

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: info-contact@alibabacloud.com 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.