利用windows api实现遍历目录

目录遍历程序

程序类型:Console

参数:目录名

要求:

1.目录遍历时,只能使用Windows API函数(FindFirstFile/FindNextFile/FindClose

2.遍历时,深度优先(用递归算法实现)

3.使用printf输出目录及文件名

4.同时将输出结果写入C:\filelist.txt

 

 1 #include<Windows.h>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 FILE* fp;
 6 void searchCatalog(char* root){
 7     char temp[100];
 8     strcpy(temp,root);
 9     strcat(temp,"\\\\*");
10     HANDLE hdnode;
11     WIN32_FIND_DATA wdfnode;
12     hdnode=FindFirstFile(temp,&wdfnode);
13     if(!FindNextFile(hdnode,&wdfnode)){
14         FindClose(hdnode);
15         return;
16     }
17     while(FindNextFile(hdnode,&wdfnode)){
18         char roottemp[100];
19         strcpy(roottemp,root);
20         strcat(roottemp,"\\\\");
21         strcat(roottemp,wdfnode.cFileName);
22         searchCatalog(roottemp);
23         printf("%s\n",wdfnode.cFileName);
24         fprintf(fp,"%s\n",wdfnode.cFileName);
25     }
26     FindClose(hdnode);
27 }
28 int main(int argc,TCHAR* argv[]){
29     if(argc<2){
30         return 0;
31     }
32     fp=fopen("C:\\filelist.txt","w");
33     if(fp==NULL)
34         return 0;
35     searchCatalog(argv[1]);
36     fclose(fp);
37 }

 

 

 

posted on 2013-12-12 15:03  云在心  阅读(1918)  评论(0)    收藏  举报

导航