遞迴降序遍歷層次目錄

2021-06-06 16:24:34 字數 2169 閱讀 2839

關於path_alloc函式::::
#include #include #include #include #include #include /* function type that is called for each filename */

typedef int myfunc(const char *, const struct stat *, int);

static myfunc myfunc;

static int myftw(char *, myfunc *);

static int dopath(myfunc *);

static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;

char*path_alloc(int* size)

intmain(int argc, char *argv)

/* * descend through the hierarchy, starting at "pathname".

* the caller's func() is called for every file.

*/#define ftw_f 1 /* file other than directory */

#define ftw_d 2 /* directory */

#define ftw_dnr 3 /* directory that can't be read */

#define ftw_ns 4 /* file that we can't stat */

static char *fullpath; /* contains full pathname for every file */

static int /* we return whatever func() returns */

myftw(char *pathname, myfunc *func)

) */

strncpy(fullpath, pathname, len); /* protect against */

fullpath[len-1] = 0; /* buffer overrun */

return(dopath(func));}/*

* descend through the hierarchy, starting at "fullpath".

* if "fullpath" is anything other than a directory, we lstat() it,

* call func(), and return. for a directory, we call ourself

* recursively for each name in the directory.

*/static int /* we return whatever func() returns */

dopath(myfunc* func)

ptr[-1] = 0; /* erase everything from slash onwards */

if (closedir(dp) < 0)

printf("can't close directory %s", fullpath);

return(ret);

}static int

myfunc(const char *pathname, const struct stat *statptr, int type)

break;

case ftw_d:

ndir++;

break;

case ftw_dnr:

printf("can't read directory %s", pathname);

break;

case ftw_ns:

printf("stat error for %s", pathname);

break;

default:

printf("unknown type %d for pathname %s", type, pathname);

} return(0);

}

遞迴降序遍歷目錄層次結構,並按檔案型別計數

本程式使用了一些對目錄進行操作的函式編寫了乙個遍歷檔案層次結構的程式,最後對各種型別的檔案計數。這個程式只有乙個引數,它說明起點路徑名,從該點開始遞迴降序遍歷檔案層次結構。其中還用到了乙個為路徑名動態分配儲存區的函式path alloc。ftw.c 2015 08 18 lucifer zhang ...

遞迴遍歷目錄

遞迴 乙個函式內部在其內部不呼叫其他函式,而是呼叫自身,類似迴圈 注,自己玩自己,防止死遞迴 使用遞迴來遍歷出乙個目錄中的所有檔案 import os def getalldirre path,sp 引數 路徑 返回值 none 完成功能,獲取呼叫者傳遞的路徑下的所有檔案 檔案 直接顯示,資料夾 二...

層次遍歷的非遞迴演算法

include include btree.h include using namespace std intmain else if q rchild null else printf n 4 二叉樹深度 d n btnodedepth p printf n 5 層次遍歷 dispbtnodebe...