首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

小弟我写的递归目录的小程序,为什么退不出循环?请大家看看

2012-03-21 
我写的递归目录的小程序,为什么退不出循环?请大家看看!#includestdio.h#includeio.h#includedirect.h

我写的递归目录的小程序,为什么退不出循环?请大家看看!
#include   <stdio.h>
#include   <io.h>
#include   <direct.h>
#include   <iostream.h>
#include   <stdlib.h>
       

void   dir(char   *buf)
{
struct   _finddata_t   c_file={0};
        long   hFile;
        if(   (hFile   =   _findfirst(   "*.* ",   &c_file   ))   ==   -1L   )  
      return;

      else  
      {  
      sprintf(buf, "%-12s\n ",c_file.name);

      if(c_file.attrib&_A_SUBDIR)
      {
          if(buf[0]!= '. ')
      {
    _chdir(buf);
    cout < < "buf: " < <buf < <endl;
    dir(buf);
      }
      }

       
                       
              while(   _findnext(   hFile,   &c_file   )   ==   0   )
              {

      sprintf(buf, "%-12s\n ",c_file.name);
      if(c_file.attrib&_A_SUBDIR)
      {

          if(buf[0]!= '. ')
      {
      cout < <buf < <endl;
              chdir(buf);
      system( "pause ");//就是这里不停,我怕大家的屏幕老闪,在这里加了一行;
              dir(buf);
      }
}

      }
                    _findclose(   hFile   );
        }
     
};


void   main()
{
char   buf[260]={0};
dir(buf);

}


[解决办法]
#include <stdio.h>
#include <io.h>
#include <direct.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>


void dir(char *target)
{
struct _finddata_t c_file={0};
char buf[260]={};
char target_[260]={0};
long hFile;
if(_chdir(target))
{
printf( "chdir failed---%s!\n ",target);
}
strcpy(target_,target);
if(target[strlen(target)]== '\\ ')
{
target_[strlen(target)] = 0;
}
sprintf(buf, "%s\\*.* ",target_);
if((hFile = _findfirst( buf, &c_file )) == -1L)
{
return;
}
else
{
printf( "directory\n ");
printf( "%s\\\n ",target_);
printf( "==============================================================\n ");
sprintf(buf, "%s ",c_file.name);
if(buf[0]!= '. ')
{
if(c_file.attrib&_A_SUBDIR)
{
char t[260];
sprintf(t, "%s\\%s ",target_,buf);
dir(t);

}
else
{
printf( "%s\\buf:%s\n ",target_,buf);


}

}
else
{
//printf( "%s\\buf:%s\n ",target_,buf);
}
while( _findnext( hFile, &c_file ) == 0)
{
sprintf(buf, "%s ",c_file.name);
if(buf[0]!= '. ')
{
if(c_file.attrib&_A_SUBDIR)
{
char t[260];
sprintf(t, "%s\\%s ",target_,buf);
dir(t);

}
else
{
printf( "%s\\buf:%s\n ",target_,buf);
}

}
else
{
//printf( "%s\\buf:%s\n ",target_,buf);
}
}
printf( "==============================================================\n ");
_findclose( hFile );
}
}

int main(int argc,char *argv[])
{
char buf[260]={0};
dir( "C:\\ ");
getche();
return 0;
}


热点排行