C语言获取目录下目录&文件列表
来源:互联网 发布:域名注册的网站名称 编辑:程序博客网 时间:2024/05/21 14:48
数据结构
struct dirent{ long d_ino; /* inode number 索引节点号 */ off_t d_off; /* offset to this dirent 在目录文件中的偏移 */ unsigned short d_reclen; /* length of this d_name 文件名长 */ unsigned char d_type; /* the type of d_name 文件类型 */ char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */}struct __dirstream { void *__fd; /* `struct hurd_fd' pointer for descriptor. */ char *__data; /* Directory block. */ int __entry_data; /* Entry number `__data' corresponds to. */ char *__ptr; /* Current pointer into the block. */ int __entry_ptr; /* Entry number `__ptr' corresponds to. */ size_t __allocation; /* Space allocated for the block. */ size_t __size; /* Total valid data in the block. */ __libc_lock_define (, __lock) /* Mutex lock for this structure. */ };typedef struct __dirstream DIR;
实现
- linux可以直接使用d_type判断是目录还是文件,win则不支持
- linux已经定义了NAME_MAX(centos6.5为255),win则需要定义,所以”#ifndef NAME_MAX”可改成”#ifdef WIN32”
#include <dirent.h>#include <string.h>//#ifdef WIN32#ifndef NAME_MAX #define NAME_MAX 255#endifint listDir( char dirName[], char buffer[][NAME_MAX], const int maxFileAmount, const int maxStringLength ){ DIR *dir; struct dirent *ptr; dir = opendir( dirName ); //open the dir int amount = 0; while((ptr = readdir(dir)) != NULL && amount < maxFileAmount){ //read the list of this dir strncpy( buffer[amount], ptr->d_name, maxStringLength ); #ifdef __linux if (ptr->d_type == 4) strcat( buffer[amount], "/" ); //add a folder marker #endif ++amount; } closedir(dir); return amount;}
0 0
- C语言获取目录下目录&文件列表
- Linux下C语言获取目录中的文件列表
- C语言 获取指定目录下的文件列表
- Linux下C语言获取目录中的文件列表
- c语言 获取目录文件列表
- C/C++--C++获取目录下的文件列表
- C/C++ 获取目录下的文件列表信息
- c语言获取文件目录
- C++获取目录下的文件列表
- C++获取目录下的文件列表
- C++获取目录下的文件列表
- 获取指定目录下的文件列表
- C++获取目录下的文件列表
- Unix下C语言----目录文件编程
- C++获取目录下所有文件列表函数
- Python 获取目录下的文件列表与内容
- c++学习之获取目录下的文件列表
- 获取目录下的文件与子目录列表
- php 正则表达式
- LeetCode326. Power of Three一行代码解决
- 【JAVA】12、循环结构
- Gradle多渠道多环境打包自动重命名
- oc加强day01作业答案
- C语言获取目录下目录&文件列表
- "0" != 0
- TortoiseSVN图标不能显示的解决方案
- CGCS2000、WGS84以及ITRF问题的阐述
- ubuntu14.04 clementine音乐播放器无法播放ape格式解决方法
- python 内置模块
- 基于Mybatis的Mysql数据库文档生成工具,支持生成docx(原创)
- nginx负载均衡篇(二)
- C语言基本概念以及关键字与标识符