DOS用C语言下对文件目录的遍历
来源:互联网 发布:c语言 取分隔符 编辑:程序博客网 时间:2024/05/18 01:18
开发环境:Turbo C 2.0
首先先看两个函数
功 能: 搜索磁盘目录; 取得下一个匹配的findfirst模式的文件
用 法: int findfirst(char *pathname, struct ffblk *ffblk, int attrib);
int findnext(struct ffblk *ffblk);
程序例:
/* findnext example */
#include <stdio.h>
#include <dir.h>
int main(void)
{
struct ffblk ffblk;
int done;
printf("Directory listing of *.* ");
done = findfirst("*.*",&ffblk,0);
while (!done)
{
printf(" %s ", ffblk.ff_name);
done = findnext(&ffblk);
}
return 0;
}
其中ffblk是一个结构体,其内容为:
char ff_reserved[21];
char ff_attrib;
unsigned ff_ftime;
unsigned ff_fdate;
long ff_fsize;
char ff_name[13];
};
ff_attrib有以下几种形式,可以做为findfirst的参数:
#define EXTENSION 0x02
#define FILENAME 0x04
#define DIRECTORY 0x08
#define DRIVE 0x10
若要遍历目录,findfirst的第三个参数应该为 0x10,则在ffblk中的ff_attrib会返回相应的属性.
void GetDirMd5(char* filePath)
{
struct ffblk fileInfo;
int done;
char filePathCpy[MAX_PATH];
char fullPath[MAX_PATH];
int tag = 0;
strcpy(filePathCpy, filePath);
done = findfirst(filePathCpy, &fileInfo, 0x10);
if(done)
{
printf("Directory not exit!/n");
return;
}
while(tag != -1 )
{
if(fileInfo.ff_attrib == 0x10) /* is a directory */
{
if( !strcmp(fileInfo.ff_name, "." ) || !strcmp(fileInfo.ff_name, ".."))
{
tag = findnext(&fileInfo );
continue;
}
strcpy(fullPath, filePathCpy);
fullPath[strlen( fullPath ) - strlen("*.*")] = '/0';
strcat(fullPath, fileInfo.ff_name);
strcat(fullPath, "//*.*");
GetDirMd5(fullPath);
}
else /* is a file */
{
strcpy(fullPath, filePath);
fullPath[strlen(fullPath) - strlen("*.*")] = '/0';
strcat(fullPath, fileInfo.ff_name);
printf("%s", fullPath);
iFileNum += 1;
}
tag = findnext(&fileInfo);
}
}
注:粘代码时代码是有'/0'竟然粘不了!!
比如你遍历一下C盘下的Test目录,只需要GetDirMd5("C://Test//*.8")就好了,呵呵.
- DOS用C语言下对文件目录的遍历
- linux下用c语言实现ls -l 对文件或者目录的属性遍历
- dos 下遍历文件目录
- C 语言递归遍历目录下的所有文件
- c语言递归遍历某个目录下的txt文件
- dos遍历目录文件
- Linux下 C语言实现遍历指定目录下指定格式的文件
- window环境下 C语言遍历一个目录下的文件
- c 语言遍历某目录下所有文件代码
- linux平台下基于C语言实现遍历文件目录
- c语言实现dos命令dir dir/s 目录遍历
- C/C++遍历目录下的文件或指定文件
- C/C++遍历目录下的文件或指定文件
- Window下使用C语言实现目录的遍历
- 【编程语言】C++ 遍历指定目录下的文件
- 用php遍历目录下的文件
- C语言遍历windows目录文件 并对文件进行读写操作
- [Linux]C语言遍历文件和目录
- 胃病面面观
- 函数对象的使用
- Error events you must handle in play flv video
- 数学 美 爱情
- 四电压比较器LM339的几个典型应用例子
- DOS用C语言下对文件目录的遍历
- 修改漫画
- Java6 JDK的安装
- 俄罗斯方块程序的另类算法
- C编写Windows服务程序详细介绍
- CRC校验8/16/32全搞定
- 了解Composite UI Application Block的基本应用
- Simple Ajax Framework
- Winpcap