struct dirent/DIR

#include <dirent.h>

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];//文件名,最长256字节

}

一般以函数strcut dirent *dp=readdir(DIR)返回来得到。

从上述也能看到dirent的内容比较少,所以dirent同样也是起着一个索引的作用,如果想获得ls -l那种效果的文件信息,必须要靠stat函数。

struct _dirstream {

void *_fd;

char *_data;

int _entry_data;

char *_ptr;

int _entry_ptr;

size_t _allocation;

size_t __size;

__libc_lock_define(, __lock);

};

typedef struct _dirstream DIR;

函数DIR* opendir(const char* pathname),打开文件目录,返回DIR结构体的指针。

具体使用函数:strcut dirent* readdir(DIR* dp);

void rewinddir(DIR *dp);

int closedir(DIR *dp);

long telldir(DIR *dp);

void seekdir(DIR* dp, long loc);

通过readdir函数读取到的文件名存储在结构体dirent的d_name中,而函数 int stat(const char *file_name, struct stat *buf);的作用就是获取文件名为d_name的文件的详细信息

strcut stat {

mode_t st_mode;//文件访问权限

ino_t st_ino;//索引节点号

dev_t st_dev;//文件使用的设备号

dev_t st_rdev;//设备文件的设备号

nlink_t st_nlink;//文件的硬连接数

uid_t st_uid;//所有者用户识别号

gid_t st_gid;//组识别号

off_t st_size;//以字节为单位的文件容量

time_t st_atime;//最后一次访问文件的时间

time_t st_mtime;//最后一次修改该文件的时间

time_t st_ctime;//最后一次改变该文件状态的时间

blksize_t st_blksize;//包含该文件的磁盘块的大小

blkcnt_t st_blocks;//该文件所占用的磁盘块

}

时间: 2025-01-07 19:24:46

struct dirent/DIR的相关文章

Linux下struct dirent,DIR,struct stat使用例子

以下例子,读取遍历目录中的文件夹,文件,并输出一些属性值,具体的相关结构体定义,可以参考: http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/ http://www.360doc.com/content/11/0110/16/4559801_85509847.shtml /* * linux struct dirent,DIR,struct stat * date: 2015-7-16 * autho

struct dirent 和DIR

1.存储目录中的文件信息(文件名.扩展名等等) #include <dirent.h> 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

关于readdir返回值中struct dirent.d_type的取值问题

原网页链接 http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html 原文及翻译 混在一起写了 unsigned char d_type This is the type of the file, possibly unknown. The following constants are defined for its value: DT_UNKNOWN The type is unknown. Only s

linux下实现简易pwd命令

/*     pwd 命令     路径名通过栈存储,先入栈存储,再出栈输出 */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <dirent.h> #include <string.h> #include <stdlib.h> #define NUM 40 /*     定义节点 */ struct node {     char name[

Android应用安全开发之源码安全

Android应用安全开发之源码安全 gh0stbo · 2016/01/21 10:24 0x00 简介 Android apk很容易通过逆向工程进行反编译,从而是其代码完全暴露给攻击者,使apk面临破解,软件逻辑修改,插入恶意代码,替换广告商ID等风险.我们可以采用以下方法对apk进行保护. 0x01 混淆保护 混淆是一种用来隐藏程序意图的技术,可以增加代码阅读的难度,使攻击者难以全面掌控app内部实现逻辑,从而增加逆向工程和破解的难度,防止知识产权被窃取. 代码混淆技术主要做了如下的工作:

linux下实现简易ll命令

/*     实现ll命令,输出指定目录下详细信息     格式:ll -l dirname     目录采用快速排序,按字典顺序排序 */ #include <stdio.h> #include <dirent.h> #include <stdlib.h> #include <sys/stat.h> #include <string.h> #include <time.h> #include <grp.h> #inclu

C语言中关于对目录的操作

原文地址:C语言中关于对目录的操作 目录的操作不论是在嵌入式产品还是应用软件编程都是必不可少的,不同的开发语言可能略有不同,笔者主要是讨论在Linux平台下对目录的一系列操作: 1.获取当前目录操作: 在系统命令行下我们可以直接输入命令:pwd 来获取当前的工作目录,但是你知道这个命令是怎么执行的吗?它是由系统通过 Shell 程序来解释执行的,在我们自己编写程序的时候怎么能获取当前的工作目录呢?在标准C库中提供了一系列关于目录操作的接口函数: char * getcwd(char * buf,

实现ls -l

就是遍历一个目录下面的所有文件,显示信息 Linux下面有点绕 要通过 opendir打开目录,返回一个DIR结构 用readdir来读DIR结构,返回目录下面的第一项,是个dirent结构体,再次调用readdir回读到第二项,以此类推 dirent结构体里面有该文件的文件名,通过stat可以获得该文件的详细信息. 注意stat结构要malloc一下,不然后segment fail??好像是这个单词.可能是没有分配的话指针会乱指. 读写执行权限在mode_t里面,没有直接输出的函数,要自己写一

linux下实现ls()函数遍历目录

需求:在linux下遍历目录,输出目录中各文件名. 在linux下遍历目录的相关函数有: #include <dirent.h> DIR* opendir(const char* dir_path); struct dirent* readdir(DIR* dirp); int closedir(DIR* dirp); int lstat(const chat* filename,struct stat* st); 在这里涉及到几个结构体:DIR,struct dirent,struct st