c语言列出文件夹中的文件

/*  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;
}
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值