C/C++的readdir和readdir_r函数(遍历目录)

1.首先要打开目录文件

DIR *opendir( const char *name);

DIR *fdopendir( int fd);

2.读取目录文件信息的函数    

注意:这是个库函数

struct dirent *readdir( DIR *dirp);

int readdir_r(    DIR *dirp,     struct dirent *entry,    struct dirent **result);

文件目录结构体:

struct dirent {
      ino_t          d_ino;       /* inode number 索引节点号*/
      off_t          d_off;       /* not an offset; see NOTES 在目录文件中的偏移*/
      unsigned short d_reclen;    /* length of this record 文件名长*/
      unsigned char  d_type;   /*type of file; not supported by all  filesystem types 文件类型*/              
                                                                                           
      char           d_name[256]; /* filename 文件名,最长255字符*/
};

d_type的值为:

  • DT_BLK This is a block device.
  • DT_CHR This is a character device.
  • DT_DIR This is a directory.
  • DT_FIFO This is a named pipe (FIFO).
  • DT_LNK This is a symbolic link.
  • DT_REG This is a regular file.
  • DT_SOCK This is a UNIX domain socket.
  • DT_UNKNOWN The file type is unknown.

readdir()函数实例:

注意:

每次使用readdir后,readdir会读到下一个文件,readdir是依次读出目录中的所有文件,每次只能读一个

这个特性和readdir_r()一样

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char **argv)
 
{
	DIR *pDir = NULL;
	struct dirent * pEnt = NULL;
	unsigned int cnt = 0;	
	if (argc != 2)
	{
		printf("usage: %s dirname\n", argv[0]);
		return -1;
	}
	pDir = opendir(argv[1]);
	if (NULL == pDir)
	{
		perror("opendir");
		return -1;
 
	}	
	while (1)
	{
		pEnt = readdir(pDir);
		if(pEnt != NULL)
		{
			if (pEnt->d_type == DT_REG)
			{
				printf("是普通文件:");
			}
			else
			{
				printf("不是普通文件:");
			}
			printf("name:[%s]	\n", pEnt->d_name);
			cnt++;
		}
		else
		{
			break;
		}
	};
	printf("总文件数为:%d\n", cnt);
	return 0;
}

结果:

$ ./a.out .
是普通文件:name:[a.c]	
不是普通文件:name:[.]	
不是普通文件:name:[..]	
是普通文件:name:[a.out]	
不是普通文件:name:[12_sr]	
不是普通文件:name:[10_sr]	
不是普通文件:name:[17_sr]	
不是普通文件:name:[15_sr]	
不是普通文件:name:[14.sr]	
不是普通文件:name:[18_sr]	
不是普通文件:name:[udp]	
不是普通文件:name:[16_sr]	
不是普通文件:name:[tcp]	
总文件数为:13

C++

#include <iostream>
#include <string>
using namespace std;
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <dirent.h>
 
void search(string commonXmlPath){
{
    string ectPath = commonXmlPath;
    string subDistrictXmlPath;
    DIR* dir_pointer = opendir(ectPath.c_str());
    if (!dir_pointer)
            return;
    for (dirent* dp = readdir(dir_pointer); dp != NULL; dp = readdir(dir_pointer))
    {
            std::string filename = dp->d_name;
            if (filename == "." || filename == "..")
            {
                   continue;
            }
                        if (dp->d_type == 8)//?录镁
                        {
                                int split = filename.find('.');
                                if (split == -1)
                                        continue;
                                string pp =  ectPath + filename;
                                cout << pp.c_str() <<"\n";
                        }
                        if(dp->d_type == 4)//目录拢卢?么录?酶碌梅                        {
                                //每赂枚募镁录?路戮露
                                subDistrictXmlPath = ectPath + string("/") + dp->d_name;
                                search(subDistrictXmlPath);
                        }
    }
    //closedir(dir_pointer);
}
int main(){
        search(string("/data/home/android/configserver/common_xmls/etc"));
        return 0;
}

readdir_r():

注意:

这三个参数

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
	DIR *pDir = NULL;
	struct dirent * pEnt = NULL;
    	struct dirent *entry = (struct dirent *)malloc(sizeof(struct dirent));
    	struct dirent **result = (struct dirent **)malloc(sizeof(struct dirent));
	unsigned int cnt = 0;
	unsigned int ret = 0;	
	if (argc != 2)
	{
		printf("usage: %s dirname\n", argv[0]);
		return -1;
	}
	pDir = opendir(argv[1]);
	if (NULL == pDir)
	{
		perror("opendir");
		return -1;
	}
	ret = readdir_r(pDir , entry , result);
	printf("return	:%d	\n", ret);
	printf("name	:[%s]	\n", entry->d_name);
	printf("name	:[%s]	\n", result[0]->d_name);
	ret = readdir_r(pDir , entry , result);
	printf("return	:%d	\n", ret);
	printf("name	:[%s]	\n", entry->d_name);
	printf("name	:[%s]	\n", result[0]->d_name);
	return 0;
 
}

结果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨痕诉清风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值