TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Michael Landrus
from: Mark Ouellet
date: 1994-12-18 16:00:14
subject: c++ recursion

Hello Michael!

Tuesday December 13 1994 11:36, Michael Landrus wrote to All:

ML> I'm trying to get a recursive directory rountine to work in C++. The
ML> following is the code I'm working with. It doesn't recurse into the
ML> subdirectories. Any ideas??

ML> //directry.cpp
ML> #include "directry.h"

ML> class Directory {
ML> public:
ML>     Directory( char * );
ML>     virtual ~Directory() {}
ML> };

ML> Directory::Directory( char *pathName)     {
ML>     struct ffblk fileBlock;
ML>     int morePathNames = !findfirst( pathName, &fileBlock, 0x17 );
ML>     while( morePathNames )
ML>         {
ML>         cout << fileBlock.ff_name << setw(10) <<
fileBlock.ff_fsize <<
ML> endl;
ML>         morePathNames = !findnext( &fileBlock );
ML>         // recurse into directories
ML>         if ((fileBlock.ff_attrib & FA_DIREC) &&
(strncmp(fileBlock.ff_name,
ML> ".", 1) != 0))
ML>              {
ML>               Directory(fileBlock.ff_name);
ML>         }
ML>     }
ML> }
ML> int main( void)    {
ML>     Directory newDirectory( "d:\*" );
ML>     return 0;
ML> }

Well I don't do C++ only Pascal, but I can see a problem with your code.

General pseudo code for recursive directory searches is:

    Initialise Dos's search record with FindFirst
    while error=0 do
        Process name:
          if a directory add to the current path and call routine again.
          else display name or call another routine to process the file etc...
    whileend
    NO MORE NAMES FOUND

Note: If the routine is called with a value parameter then all is fine, if
it's called with a reference then each recursive call is modifying the same
copy of our path variable so take care of this. Also might be easier to
call the routine with "OldPath with [*.*] removed" + Dir found +
"\*.*" this way you are not actually modifying your current
parameter and you don't have anything to do to it when you return from the
recursive calls, unless as I said you use a reference parameter.

    FindFirst/Next also rely on DOS routines. These routines have a
peculiarity. They return ALL NORMAL files PLUS those with attributes
specified. So a search with Directory as attribute will return all normal
files AND directories. Your value "0x17" corresponds to what
Borland Pascal defines as AnyFile which is good. You'll get everything,
hidden and all.

    So your only problem I can see is that you call FindNext before
checking the name you currently have is a directory. You'll probably be
missing a few in the lot.

    Allways do the FindNext as the ABSOLUTE LAST step in your routine. Only
when you have fully processed the current name.

    Best regards,
    Mark Ouellet.

...The degree of technical competence is inversely proportional
...to the level of management.
--- GoldED/2 2.50.B1016+[45LM2]
* Origin: Mark's point under OS/2 2.11 Sillery, Qc, Canada (1:240/99.44)
SEEN-BY: 12/2442 620/243 632/348 640/820 690/660 711/409 410 413 430 807 808
SEEN-BY: 711/809 934 942 949 712/353 515 713/888 800/1
@PATH: 240/99 12/12 3615/50 229/2 12/2442 711/409 808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.