FTW function file acquisition

Source: Internet
Author: User

Function Description:

Int FTW (const char * dirpath,
INT (* fN) (const char * fpath, const struct stat * Sb, int typeflag), int depth );

FTW () recursively traverses sub-directories from the directory specified by dirpath. FTW () will pass three parameters to FN (). The first parameter * fpath points to the current directory path, and the second parameter is * Sb, which is the stat structure pointer, the third parameter is flag, which has the following possible values:

Ftw_f common file

Ftw_d directory

A directory that cannot be read by ftw_dnr.

Ftw_sl symbolic connection

Ftw_ns cannot obtain the stat structure data, which may be caused by permission issues.

The last depth parameter indicates the number of files opened by FTW () during directory traversal. FTW () requires at least one file descriptive word in each layer of the time duration. If the time duration is used up, the whole traversal will be slow because the file and open file operations are constantly closed.

If you want to end the traversal of FTW (), FN () Only returns a non-zero value, which is also the return value of FTW. Otherwise, FTW () tries to finish all the directories and returns 0

The following describes a simple program:

# Include <FTW. h> # include <stdio. h> # include <sys/STAT. h> # include <unistd. h> int FN (const char * fpath, const struct stat * Sb, int typeflag); int main () {int temp = FTW ("/root/leeboy", FN, 500); // when the returned value is 0, it indicates that the printf ("% d \ n", temp); Return 0;} int FN (const char * fpath, const struct stat * Sb, int typeflag) {// view the file type and output the file device number if (typeflag = ftw_f) {printf ("standard file: \ n "); printf (" Dev: % d \ n ", Sb-> st_dev);} else if (typeflag = ftw_d) {printf ("folder path \ n"); printf ("Dev: % d \ n", Sb-> st_dev);} printf ("% s \ n ", fpath); Return 0; // If the returned number is not 0, the program terminates the return, and the main program FTW returns the same value}

Program running result:

Returns the result after the traversal is complete.

Modify the return value:

Only one layer is returned, and the return value is the same as the FN return value.

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.