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.