C语言遍历文件夹里所有文件【转】

 1 #include<iostream>   
 2   #include<string>   
 3   #include<io.h>   
 4   using namespace std; 
 5 
 6   void   filesearch(string path,int layer)   
 7   {   
 8           struct _finddata_t   filefind;   
 9           string  curr=path+"\\*.*";   
10           int   done=0,i,handle;   
11           if((handle=_findfirst(curr.c_str(),&filefind))==-1)
12               return; 
13                 
14           while(!(done=_findnext(handle,&filefind)))   
15           {   
16                       printf("测试的--%s\n",filefind.name);
17                   if(!strcmp(filefind.name,"..")){
18                       
19                           continue;
20                   }
21                   
22                   for(i=0;i<layer;i++)
23                           cout<<"     "; 
24                 
25                 
26                 
27                     
28                   if   ((_A_SUBDIR==filefind.attrib))   
29                   {           
30                               printf("----------%s\n",filefind.name);    
31                           cout<<filefind.name<<"(dir)"<<endl;   
32                           curr=path+"\\"+filefind.name;   
33                          filesearch(curr,layer+1);   
34                   }   
35                   else     
36                   {   
37                           cout<<filefind.name<<endl;   
38                   }   
39           }           
40           _findclose(handle);               
41   }   
42   int   main()   
43   {           
44           string   path;   
45           cout<<"请输入目录"<<endl;   
46           cin>>path;   
47           filesearch(path,0);   
48           system("PAUSE");   
49           return   0;   
50   }   
51