Recursive directory tree reading
Quote:
> Marco Quezada a crit dans le message ...
> >Hello all,
> >I am writing a routine in C that will load a directory tree
> >structure.
> The C language does not know what a directory is.
> >It is similar to the tree command in linux but specific to my
> >needs do
> >I can't and don't want to use that source. So far I've had some
> >trouble so I was wondering if anyone else has tried something like
> >this and could point me to a reference or give a source example.
> Your C compiler, that is dedicated to your machine+OS combination,
> has probably an extension for that. Read the manual, there is
> probably an example. If you are stuck, ask in a newsgroup dedicated
> to your machine+OS+compiler.
> Personal opinion: Avoid recursivity.
> - It makes hard to read, debug and maintain programs.
> - It makes program that are dependend from the size of the
> automatic memory.
> - There is no way to prevent a crash against a too small
> automatic memory.
I think this represents a fairly common C design problem, i.e. a
naturally recursive system that MAY need fairly large storage in
SOME instances. This is typical of directory structures, where
many have only a few entries, and a few may have hundreds or more.
I have handled this in the past with something like:
int recurse(int param)
{
while (whatever) {
sz = computelocalsizeneeded()
ptr = malloc(sz);
....
recurse(asneeded)
free(ptr);
}
Quote:
}
Which gets the large storage out of the (possibly limited) stack,
and maintains the clarity of recursive solution. Everything
remains nicely re-entrant.
The localsize/malloc may be handled by building a locally headed
list, and the free operation would have to walk the list to free it.
IMHO too many people reject a query because it is fairly specific,
and thus appears to be off topic. Abstracting things is instructive.
--
http://www.*-*-*.com/
--