程序代码:
#include <cstdio>
#include <cstring>
#include "io.h"
#define MAX_PATH 260
void find_file(char *dir,FILE * &lpFile)
{
char szDir[MAX_PATH]={0};
sprintf(szDir,"%s\\*.*",dir);
_finddata_t file;
long re = _findfirst(szDir,&file);
if( -1l == re )/////////无文件
return;
while( _findnext( re,&file ) == 0)////遍历文件
{
if(strcmp(file.name,".") == 0/*过滤.目录*/ || strcmp(file.name,"..") == 0/*过滤..目录*/)
continue;
if( file.attrib != _A_SUBDIR )
{
fprintf(lpFile,"%s\\%s\n",dir,file.name);
}
else
{
char szSubDir[MAX_PATH]={0};
sprintf(szSubDir,"%s\\%s",dir,file.name);
find_file(szSubDir,lpFile);
}
}
}
int main()
{
char * dir = "C:\\Windows";//注意末尾不要加“\\”
FILE * lpFile = fopen("files.txt","w");
if(lpFile)
{
find_file(dir,lpFile);
fclose(lpFile);
}
return 0;
}
[
本帖最后由 zxwangyun 于 2012-6-11 15:59 编辑 ]