単純な「ファイルツリーウォーク」については、man ftw
を参照してください。この例ではfnmatch
も使用しました。
#include <ftw.h>
#include <fnmatch.h>
static const char *filters[] = {
"*.jpg", "*.jpeg", "*.gif", "*.png"
};
static int callback(const char *fpath, const struct stat *sb, int typeflag) {
/* if it's a file */
if (typeflag == FTW_F) {
int i;
/* for each filter, */
for (i = 0; i < sizeof(filters)/sizeof(filters[0]); i++) {
/* if the filename matches the filter, */
if (fnmatch(filters[i], fpath, FNM_CASEFOLD) == 0) {
/* do something */
printf("found image: %s\n", fpath);
break;
}
}
}
/* tell ftw to continue */
return 0;
}
int main() {
ftw(".", callback, 16);
}
(でもテストコンパイルが、あなたのアイデアを得るわけではありません。)
これはDIRENT
sおよび再帰的なトラバースを自分で対処するよりもはるかに簡単です。
トラバーサルをより詳細に制御するには、fts
もあります。この例では、プログラムに開始点として明示的に渡されない限り、ドットファイル(名前が "。"で始まるファイルとディレクトリ)はスキップされます。
#include <fts.h>
#include <string.h>
int main(int argc, char **argv) {
char *dot[] = {".", 0};
char **paths = argc > 1 ? argv + 1 : dot;
FTS *tree = fts_open(paths, FTS_NOCHDIR, 0);
if (!tree) {
perror("fts_open");
return 1;
}
FTSENT *node;
while ((node = fts_read(tree))) {
if (node->fts_level > 0 && node->fts_name[0] == '.')
fts_set(tree, node, FTS_SKIP);
else if (node->fts_info & FTS_F) {
printf("got file named %s at depth %d, "
"accessible via %s from the current directory "
"or via %s from the original starting directory\n",
node->fts_name, node->fts_level,
node->fts_accpath, node->fts_path);
/* if fts_open is not given FTS_NOCHDIR,
* fts may change the program's current working directory */
}
}
if (errno) {
perror("fts_read");
return 1;
}
if (fts_close(tree)) {
perror("fts_close");
return 1;
}
return 0;
}
また、コンパイルテストも実行テストもされていませんが、私はそれについて言及したいと思いました。
どのオペレーティングシステムですか? –