#include <stdio.h>
#include <string.h>
#include <io.h>
#include <stdlib.h>
//最后有相关函数讲解
//将数组myarray的内容复制到数组S中
void initarray(char s[],char myarray[])
{
int i;
for(i=0;i<strlen(myarray);i++)
{
s[i]=myarray[i];
}
s[i]='\0';
strcat(s,"\\");
}
//递归的列出文件夹下所有的子文件夹和文件
//参数path:要进行查找的目录路径
void queryfolder(char path[])
{
struct _finddata_t FileInfo; //_finddata_t是文件信息结构体
long Handle;
char str1[256],str2[256];
initarray(str1,path);
strcat(str1,"*");
initarray(str2,path);
if((Handle=_findfirst(str1,&FileInfo))==-1L) //查找目录中符合条件的文件
printf("没有找到\n");
else
{
//最先找到的是当前文件夹".",所以不用处理
while(!_findnext(Handle,&FileInfo))
{
//printf("%s\n",FileInfo.name);
if((FileInfo.attrib & _A_SUBDIR)==16 && strcmp(FileInfo.name,".."))
{
//printf("%s是文件夹\n",FileInfo.name);
strcat(str2,FileInfo.name);
printf("子文件夹:%s\n",str2);
queryfolder(str2);
}
else if(!(FileInfo.attrib & _A_SUBDIR))
{
strcat(str2,FileInfo.name);
printf("文件:%s\n",str2);
}
initarray(str2,path);
}
_findclose(Handle);
}
}
int main(void)
{
char fpath[256]="E:\\天下足球";
queryfolder(fpath);
return 0;
}
/*******
struct _finddata_t 是用来存储文件各种信息的结构体
定义如下:
struct _finddata_t
{ unsigned attrib; //文件属性
time_t time_create; //创建时间
time_t time_access; //文件最后一次被访问时间按
time_t time_write; //文件最后一次被修改时间
_fsize_t size; //文件大小
char name[_MAX_FNAME]; //文件名
};
unsigned atrrib:文 件属性的存储位置。它存储一个unsigned单元,用于表示文件的属性。
文件属性是用位表示的,主要有以下一些:_A_ARCH(存档)、 _A_HIDDEN(隐藏)、
_A_NORMAL(正常)、_A_RDONLY(只读)、_A_SUBDIR(文件夹)、_A_SYSTEM(系统)。
这 些都是在<io.h>中定义的宏,可以直接使用,而本身的意义其实是一个无符号整型(只不过这个整型应该是2的几次幂,从而保证只有一位为 1,而其他位为0)。
既然是位表示,那么当一个文件有多个属性时,它往往是通过位或的方式,
来得到几个属性的综合。例如只读+隐藏+系统属性,
应该为:_A_HIDDEN | _A_RDONLY | _A_SYSTEM 。
*********/
/********
函数名: strcat
功 能: 字符串拼接函数
用 法: char *strcat(char *destin, char *source);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char destination[25];
char *blank = " ", *c = "C++", *Borland = "Borland";
strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);
printf("%s\n", destination);
return 0;
}
*********/
/********
函数名: findfirst, findnext
功 能: 搜索磁盘目录; 取得下一个匹配的findfirst模式的文件
用 法: int findfirst(char *pathname, struct ffblk *ffblk, int attrib);
int findnext(struct ffblk *ffblk);
程序例: findnext()
#include <stdio.h>
#include <dir.h>
int main(void)
{
struct ffblk ffblk;
int done;
printf("Directory listing of *.*\n");
done = findfirst("*.*",&ffblk,0);
while (!done)
{
printf(" %s\n", ffblk.ff_name);
done = findnext(&ffblk);
}
return 0;
}
*********/
/********
_findfirst, _findnext, _findclose三个函数在下一篇博文中讲述
********/
#include <string.h>
#include <io.h>
#include <stdlib.h>
//最后有相关函数讲解
//将数组myarray的内容复制到数组S中
void initarray(char s[],char myarray[])
{
int i;
for(i=0;i<strlen(myarray);i++)
{
s[i]=myarray[i];
}
s[i]='\0';
strcat(s,"\\");
}
//递归的列出文件夹下所有的子文件夹和文件
//参数path:要进行查找的目录路径
void queryfolder(char path[])
{
struct _finddata_t FileInfo; //_finddata_t是文件信息结构体
long Handle;
char str1[256],str2[256];
initarray(str1,path);
strcat(str1,"*");
initarray(str2,path);
if((Handle=_findfirst(str1,&FileInfo))==-1L) //查找目录中符合条件的文件
printf("没有找到\n");
else
{
//最先找到的是当前文件夹".",所以不用处理
while(!_findnext(Handle,&FileInfo))
{
//printf("%s\n",FileInfo.name);
if((FileInfo.attrib & _A_SUBDIR)==16 && strcmp(FileInfo.name,".."))
{
//printf("%s是文件夹\n",FileInfo.name);
strcat(str2,FileInfo.name);
printf("子文件夹:%s\n",str2);
queryfolder(str2);
}
else if(!(FileInfo.attrib & _A_SUBDIR))
{
strcat(str2,FileInfo.name);
printf("文件:%s\n",str2);
}
initarray(str2,path);
}
_findclose(Handle);
}
}
int main(void)
{
char fpath[256]="E:\\天下足球";
queryfolder(fpath);
return 0;
}
// 以下内容为函数讲解
/*******
struct _finddata_t 是用来存储文件各种信息的结构体
定义如下:
struct _finddata_t
{ unsigned attrib; //文件属性
time_t time_create; //创建时间
time_t time_access; //文件最后一次被访问时间按
time_t time_write; //文件最后一次被修改时间
_fsize_t size; //文件大小
char name[_MAX_FNAME]; //文件名
};
unsigned atrrib:文 件属性的存储位置。它存储一个unsigned单元,用于表示文件的属性。
文件属性是用位表示的,主要有以下一些:_A_ARCH(存档)、 _A_HIDDEN(隐藏)、
_A_NORMAL(正常)、_A_RDONLY(只读)、_A_SUBDIR(文件夹)、_A_SYSTEM(系统)。
这 些都是在<io.h>中定义的宏,可以直接使用,而本身的意义其实是一个无符号整型(只不过这个整型应该是2的几次幂,从而保证只有一位为 1,而其他位为0)。
既然是位表示,那么当一个文件有多个属性时,它往往是通过位或的方式,
来得到几个属性的综合。例如只读+隐藏+系统属性,
应该为:_A_HIDDEN | _A_RDONLY | _A_SYSTEM 。
*********/
/********
函数名: strcat
功 能: 字符串拼接函数
用 法: char *strcat(char *destin, char *source);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char destination[25];
char *blank = " ", *c = "C++", *Borland = "Borland";
strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);
printf("%s\n", destination);
return 0;
}
*********/
/********
函数名: findfirst, findnext
功 能: 搜索磁盘目录; 取得下一个匹配的findfirst模式的文件
用 法: int findfirst(char *pathname, struct ffblk *ffblk, int attrib);
int findnext(struct ffblk *ffblk);
程序例: findnext()
#include <stdio.h>
#include <dir.h>
int main(void)
{
struct ffblk ffblk;
int done;
printf("Directory listing of *.*\n");
done = findfirst("*.*",&ffblk,0);
while (!done)
{
printf(" %s\n", ffblk.ff_name);
done = findnext(&ffblk);
}
return 0;
}
*********/
/********
_findfirst, _findnext, _findclose三个函数在下一篇博文中讲述
********/