user975582
user975582

Reputation: 205

Traverse Directory Depth First

I need to traverse a directory depth first without using boost but I have not been able to find a good tutorial how to do this. I know how to list the files of the directory, but not sure how to about this one. This list the files of a directory:

Upvotes: 3

Views: 528

Answers (2)

Michał Šrajer
Michał Šrajer

Reputation: 31182

Make sure you understand recursion.

I assume you have a function walk(dir_path) which can list all files (and directries) in the dir_path directory. You need to modify it, so it calls it self (recursively) for each directory you find. That's it.

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363737

Use the ftw or nftw functions if your system has them. Or, grab the fts_* functions from, e.g., the OpenBSD source tree and study those, or use them directly. This problem is harder than you might think, because you can run out of file descriptors when recursing through deep filesystem hierarchies.

Upvotes: 1

Related Questions