C语言下文件目录遍历通常会用到下面这些函数

  _access()        /* 判断文件或文件夹路径是否合法 */

  _chdir()     /* 切换当前工作目录 */

  _findfirst()    /* 查找第一个符合要求的文件或目录 */

  _findnext()     /* 查找下一个 */

  _findclose()    /* 关闭查找 */

与此同时还会使用到 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[FILENAME_MAX];       /* 表示文件的名称 */

};

文件属性(attrib)的值可以取下面的值:

#define _A_NORMAL 0x00000000
         #define _A_RDONLY 0x00000001
         #define _A_HIDDEN 0x00000002
         #define _A_SYSTEM 0x00000004
         #define _A_VOLID 0x00000008
         #define _A_SUBDIR 0x00000010
         #define _A_ARCH  0x00000020

在io.h文件中FILENAME_MAX 被定义为260

下面给出的是一个简单的小程序用于列出目录C:\ 下的文件夹的名字

(这里建议大家使用斜杠'/',少用'\',windows下程序能够自动解析'/',使用反斜杠时需要使用"\\")

 #include <dir.h>
#include <stdio.h>
#include <io.h>
#include <string.h> int main(int argc, char* argv[])
{
char path[] = "C:/"; struct _finddata_t fa;
long handle; if((handle = _findfirst(strcat(path,"*"),&fa)) == -1L)
{
printf("The Path %s is wrong!\n",path);
return ;
} do
{
if( fa.attrib == _A_SUBDIR && ~strcmp(fa.name,".")&& ~strcmp(fa.name,".."))
printf("The subdirectory is %s\n",fa.name);
}while(_findnext(handle,&fa) == ); /* 成功找到时返回0*/ _findclose(handle); return ;
}

最新文章

  1. 转发:Chrome 控制台console的用法
  2. WebApi系列~QQ互联的引入(QConnectSDK)
  3. webkit的一些不为人知的高级属性
  4. jquery操作html data-* 属性的坑
  5. 30款jQuery常用网页焦点图banner图片切换 下载
  6. Autowired properities class
  7. hdu 4712
  8. javascript获取随机颜色
  9. OpenJudge / Poj 1928 The Peanuts C++
  10. C# 创建验证码图片
  11. 搭建hbase-0.94.26集群环境
  12. Google机器学习教程心得(一)
  13. kali 2017更新源
  14. ASP.NET Core 2.0 : 六. 举个例子来聊聊它的依赖注入
  15. JAVA_SE基础——13.选择结构语句
  16. 大名鼎鼎的Requests库用了什么编码风格?
  17. 使用json读写文件中的数据
  18. offsetHeight,clientHeight,scrollHeight,offsetY等属性的理解
  19. 23 模块 os sys pickle json
  20. AP与CP介绍【转】

热门文章

  1. php 代码的执行
  2. Retrofit学习笔记(一)
  3. 【BZOJ2034】[2009国家集训队]最大收益 贪心优化最优匹配
  4. String、StringBuffer与StringBuilder的区别。
  5. JS实现过一段时间后清理数据(以Lable为例)
  6. Error: member names cannot be the same as their enclosing type
  7. Python 通过字符串调用函数、接近属性
  8. Python 网络编程——socket
  9. 基于TCP_socket套接字实现远程执行命令
  10. Windows Server 2003 R2 With Sp2 序列号