1、DIR,是directory的缩写,是思。DIR也是是DOS操作系统用来查看磁盘中文件的命令dir有很多的参数。
2、头文件:#include <dirent.h>
3、对应结构体如下:
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;
{
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;
4、struct dirent结构体相应的函数:opendir(),readdir(),closedir()
opendir():
头文件:#include <sys/types.h>
#include <dirent.h>
函数原型
DIR* opendir (const char * path );
函数功能:打开一个目录,在失败的时候返回一个空的指针。
readdir():
语法: struct dirent* readdir(DIR* dir_handle);
返回值: dirent的结构类型
函数种类: 文件存取
closedir():
closedir()关闭参数dir所指的目录流。关闭成功则返回0,失败返回-1,错误原因存于errno 中。EBADF 参数dir为无效的目录流。
注意:目录文件作为一种文件,在打开必须关闭,否则会由于文件的进程打开文件过多而不能打开新的文件。因此opendir函数和closedir函数同样是配对出现的。
5、示例程序:
程序功能:从当前目录的文件夹test中读取所有文件,把所有文件的文件名存放在链表上,输出打印。对每一个对文件进行读操作,按照行进行操作,在屏幕上打印文件里的字符串。
源程序如下:
#include <stdio.h>
#include <dirent.h>
/*#include <alloc.h>*/
#include <string.h>
#include <stdlib.h>
#if 0
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;
#endif
int main(int argc,char *argv[])
{
DIR *directory_pointer; //DIR是directory的缩写,是目录的意思
struct dirent *entry;
struct FileList
{
char filename[64];
struct FileList *next;
}start,*node;
FILE *fp = NULL;
char buffer[20]="\0";
if (argc!=2)
{
printf("Must specify a directory\n");
exit(1);
}
if ((directory_pointer=opendir(argv[1]))==NULL)
printf("Error opening %s\n",argv[1]);
else
{
start.next=NULL;
node=&start;
while ((entry=readdir(directory_pointer))!=NULL)
{
node->next=(struct FileList *)malloc(sizeof(struct FileList));
node=node->next;
node->next = NULL;
strcpy(node->filename,entry->d_name);
if(strcmp(node->filename,".")==0||strcmp(node->filename,"..")==0)
{
//printf("node->filename error\n");
continue ;
}
else
{
char path[50]="./test/";
strcat(path,node->filename);
printf("filename=%s\n",node->filename);
printf("path=%s\n",path);
fp = fopen(path,"r");
if(fp == NULL)
{
printf("%d\n",fp);
printf("cannot open this file!\n");
continue;
}
while(!feof(fp))
{
if(fgets(buffer,127,fp) == NULL)
break;
buffer[strlen(buffer)-1] = '\0';
printf("%s\n",buffer);
}
}
node->next=NULL;
}
closedir(directory_pointer);
node=start.next;
while(node)
{
printf("%s\n",node->filename);
node=node->next;
}
}
system("pause");
return 0;
}