/* TC2.0 下编译*/
#include "stdio.h"
#include "stdlib.h"
#include "dir.h"
#include "dos.h"
#define wait() getch()
/*
============目录函数(原型声明所在头文件为dir.h、dos.h)================
int chdir(char *path) 使指定的目录path(如:"C://WPS")变成当前的工作目录,成
功返回0
int findfirst(char *pathname,struct ffblk *ffblk,int attrib)查找指定的文件,成功
返回0
pathname为指定的目录名和文件名,如"C://WPS//TXT"
ffblk为指定的保存文件信息的一个结构,定义如下:
┏━━━━━━━━━━━━━━━━━━┓
┃struct ffblk ┃
┃{ ┃
┃ char ff_reserved[21]; //DOS保留字 ┃
┃ char ff_attrib; //文件属性 ┃
┃ int ff_ftime; //文件时间 ┃
┃ int ff_fdate; //文件日期 ┃
┃ long ff_fsize; //文件长度 ┃
┃ char ff_name[13]; //文件名 ┃
┃} ┃
┗━━━━━━━━━━━━━━━━━━┛
attrib为文件属性,由以下字符代表
┏━━━━━━━━━┳━━━━━━━━┓
┃FA_RDONLY 只读文件┃FA_LABEL 卷标号┃
┃FA_HIDDEN 隐藏文件┃FA_DIREC 目录 ┃
┃FA_SYSTEM 系统文件┃FA_ARCH 档案 ┃
┗━━━━━━━━━┻━━━━━━━━┛
例:
struct ffblk ff;
findfirst("*.wps",&ff,FA_RDONLY);
int findnext(struct ffblk *ffblk) 取匹配finddirst的文件,成功返回0
*/
void formatExt(char *dstr,char *sstr)
{
sscanf(sstr,"[^///:*?/"<>|]",dstr);
}
typedef int (*METHOD)();
int enum_allFile(METHOD method,char *dir,char *type,int filter)
{/*
method==NULL 仅显示文件名
dir==NULL 当前文件夹
*/
static struct ffblk ff;
static int ct,run_method;
run_method=0;
if(type!=NULL){
if(dir!=NULL) {
if(chdir(dir)){
puts("folder can't found!");
wait();
exit(1);
}
}
formatExt(type,type);
if(findfirst(type,&ff,filter)) {
puts("can't find file!");
ct=0;
return 0;
}
ct=1;
run_method=1;
}
else {
if(findnext(&ff) ==0){
ct++;
run_method=1;
}
else return ct;
}
if(run_method){
if(method!=NULL) method(ff.ff_name);
else printf("/n%s",ff.ff_name);
}
enum_allFile( method,NULL, NULL, 0);
return ct;
}
void TestFunc(char *fname)
{
puts(fname);
}
int main()
{
int ct;
ct=enum_allFile( TestFunc, NULL, "*.c", FA_RDONLY | FA_HIDDEN | FA_SYSTEM );/* */
printf("/n--------------%d-------------/n",ct);
wait();
return 0;
}