Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Members of Dirent structure

I have started working with dirent.h library and I came across a very useful member of "struct dirent" structer which struct dirent *p->d_name in my book. But unfortunatly it doesn't states any other members of this structure;

I was wondering what else are the members of this structure and what are they used for?

Regards

like image 934
Naruto Avatar asked Oct 20 '12 18:10

Naruto


People also ask

What is struct dirent in C++?

Data Type: struct dirent. This is a structure type used to return information about directory entries. It contains the following fields: char d_name[] This is the null-terminated file name component.

Why dirent H is used?

h file, is used for directory access operations. Using these access operations and the dirent structure, along with its associated constants and macros, shields you from the details of implementing a directory and provides a consistent interface to directories across all types of file systems.

What does Readdir do in C?

The readdir() function returns a pointer to a structure representing the directory entry at the current position in the directory stream specified by the argument dirp, and positions the directory stream at the next entry. It returns a null pointer upon reaching the end of the directory stream.

How do you use Readdir?

The readdir() function reads the next directory entry from the directory specified by dirp, which is the value returned by a call to opendir(). You can call readdir() repeatedly to list all of the entries contained in the directory specified by the pathname given to opendir().


3 Answers

The structure, struct dirent refers to directory entry.

http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html

In linux it is defined as:

struct dirent {     ino_t          d_ino;       /* inode number */     off_t          d_off;       /* offset to the next dirent */     unsigned short d_reclen;    /* length of this record */     unsigned char  d_type;      /* type of file; not supported                                    by all file system types */     char           d_name[256]; /* filename */ }; 

refer: man readdir

Or just look for "dirent.h" in the include directory.

like image 180
askmish Avatar answered Sep 20 '22 12:09

askmish


There are only two members (from wikipedia):

  • ino_t d_ino - file serial number
  • char d_name[] - name of entry (will not exceed a size of NAME_MAX)

Take a look at the unix spec as well.

like image 21
MByD Avatar answered Sep 18 '22 12:09

MByD


in addition to above answer of @Binyamin Sharet:

 off_t d_off - file offset
    unsigned short int d_reclen - length of the dirent record
    unsigned short int d_namlen - length of name
    unsigned int d_type - type of file
like image 21
Ravindra Bagale Avatar answered Sep 18 '22 12:09

Ravindra Bagale