linux opendir和readdir的使用


1    opendir

#include
#include

DIR *opendir(const char *name);


传入name路径,成功则返回非空DIR指针,否则返回NULL


2    readdir


     #include


       struct dirent *readdir(DIR *dirp);


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


readdir一般要配合opendir使用,readdir不是线程安全函数,代替他的有readdir_r。

readdir返回 struct dirent *指针,读完目录下所有文件时,返回NULL


如果系统支持readdir_r,建议用readdir_r , readdir_r成功返回0。


关于struct dirent结构体:

 On Linux, the dirent structure is defined as follows:


  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 */
  };


3    closedir

      #include


       #include


       int closedir(DIR *dirp);

closedir配合opendir使用。



#include 
#include 
#include 
#include 	
#include 
#include 
//DIR *opendir(const char *name);


#ifndef  LOG_TRACE
#define LOG_TRACE printf
#define LOG_INFO(msg)  \
	do{ \
		LOG_TRACE msg; \
		LOG_TRACE("[%s %d] \n",__FUNCTION__,__LINE__);\
	}while(0)

#define LOG_ERROR(err_info)  \
	do{ \
		LOG_TRACE err_info; \
		LOG_TRACE("[%s %d]  \n",__FUNCTION__,__LINE__);\
	}while(0)
#endif

extern int errno;
int open_dir_1(const char *pDirname)
{

	DIR * dirp = NULL; 
	struct dirent * pDirent= NULL;
	if((NULL == pDirname) ||
		(0 == strlen(pDirname)))
	{
		LOG_ERROR(("param error"));
		return -1;
	}
	
	dirp = opendir(pDirname);
	if (NULL == dirp)
	{
		LOG_ERROR(("opendir %s failed! error_no: %s",pDirname , strerror(errno)));
		return -1;
	}
	
	while (NULL != (pDirent = readdir(dirp)))
	{
	
		if (pDirent->d_type == DT_DIR )
		{
			LOG_ERROR(("dir      [%s]       ",pDirent->d_name));
		}
		else if(pDirent->d_type == DT_REG)
		{
			LOG_ERROR(("file     [%s]        ",pDirent->d_name));
		}

	}

	closedir(dirp);
	return 0;
}

int open_dir_2(const char *pDirname)

{

	DIR * dirp = NULL; 
	struct dirent * pDirent= NULL;
	struct dirent *pStResult = NULL;
	
	if((NULL == pDirname) ||
		(0 == strlen(pDirname)))
	{
		LOG_ERROR(("param error"));
		return -1;
	}
	dirp = opendir(pDirname);
	if (NULL == dirp)
	{
		LOG_ERROR(("opendir %s failed! error_no: %s",pDirname , strerror(errno)));
		return -1;
	}

	pDirent = (struct dirent *)malloc(sizeof(struct dirent));
	if(!pDirent)
	{
		LOG_ERROR(("pDirent error"));
		closedir(dirp);
		return -1;
	}
	while (( 0== readdir_r(dirp,pDirent,&pStResult))&&
		(pStResult != NULL))
	{
		if (pDirent->d_type == DT_DIR )
		{
			LOG_ERROR(("dir      [%s]       ",pDirent->d_name));
		}
		else if(pDirent->d_type == DT_REG)
		{
			LOG_ERROR(("file     [%s]        ",pDirent->d_name));
		}

	}

	closedir(dirp);
	
	return 0;
}


int main()
{
	LOG_ERROR(("***********"));
	open_dir_1("/share/");
	LOG_ERROR(("------------"));
	open_dir_2("/share/");

	LOG_ERROR(("-++--+++--"));
	return 0;
}


运行结果:


./a.out [19@gcc test_opendir_readdir.c[C
root@ubuntu:/share# 
root@ubuntu:/share# 
root@ubuntu:/share# gcc test_opendir_readdir.c 
/a.out 
***********[main 113]  
file     [zlib-1.2.8.tar.gz]        [open_dir_1 54]  
file     [1.tmp]        [open_dir_1 54]  
file     [log.c]        [open_dir_1 54]  
file     [list.c]        [open_dir_1 54]  
dir      [11]       [open_dir_1 50]  
file     [test.out]        [open_dir_1 54]  
file     [test_system_func.c]        [open_dir_1 54]  
file     [a.out]        [open_dir_1 54]  
file     [test_strncpy.c]        [open_dir_1 54]  
file     [test_proc_partitions.c]        [open_dir_1 54]  
file     [test.c]        [open_dir_1 54]  
file     [New0001.c]        [open_dir_1 54]  
dir      [curl-7.51.0]       [open_dir_1 50]  
file     [test_ftok.c]        [open_dir_1 54]  
file     [log.h]        [open_dir_1 54]  
file     [test_gettimeofday.c]        [open_dir_1 54]  
file     [csdn.c]        [open_dir_1 54]  
file     [client.c]        [open_dir_1 54]  
file     [1.txt]        [open_dir_1 54]  
file     [test_opendir_readdir.c]        [open_dir_1 54]  
file     [curl-7.51.0.tar.gz]        [open_dir_1 54]  
file     [2.txt]        [open_dir_1 54]  
file     [opendir.c]        [open_dir_1 54]  
dir      [zlib-1.2.8]       [open_dir_1 50]  
file     [123.rmvb]        [open_dir_1 54]  
dir      [ffmpeg_learn]       [open_dir_1 50]  
file     [test_sem.c]        [open_dir_1 54]  
file     [test_list.c]        [open_dir_1 54]  
file     [list.h]        [open_dir_1 54]  
file     [server]        [open_dir_1 54]  
dir      [ProFFmpeg]       [open_dir_1 50]  
dir      [ffmpeg-3.1.6]       [open_dir_1 50]  
dir      [yasm-1.3.0]       [open_dir_1 50]  
file     [New0003.c]        [open_dir_1 54]  
dir      [.]       [open_dir_1 50]  
dir      [..]       [open_dir_1 50]  
file     [ffmpeg-3.1.6.tar.gz]        [open_dir_1 54]  
file     [types.h]        [open_dir_1 54]  
file     [New0002.c]        [open_dir_1 54]  
file     [simple.out]        [open_dir_1 54]  
file     [server.c]        [open_dir_1 54]  
dir      [learn]       [open_dir_1 50]  
file     [simple_ffmpeg_player.c]        [open_dir_1 54]  
file     [client]        [open_dir_1 54]  
dir      [abc]       [open_dir_1 50]  
file     [yasm-1.3.0.tar.gz]        [open_dir_1 54]  
file     [output.yuv]        [open_dir_1 54]  
file     [mySDLFirst.out]        [open_dir_1 54]  
------------[main 115]  
file     [zlib-1.2.8.tar.gz]        [open_dir_2 100]  
file     [1.tmp]        [open_dir_2 100]  
file     [log.c]        [open_dir_2 100]  
file     [list.c]        [open_dir_2 100]  
dir      [11]       [open_dir_2 96]  
file     [test.out]        [open_dir_2 100]  
file     [test_system_func.c]        [open_dir_2 100]  
file     [a.out]        [open_dir_2 100]  
file     [test_strncpy.c]        [open_dir_2 100]  
file     [test_proc_partitions.c]        [open_dir_2 100]  
file     [test.c]        [open_dir_2 100]  
file     [New0001.c]        [open_dir_2 100]  
dir      [curl-7.51.0]       [open_dir_2 96]  
file     [test_ftok.c]        [open_dir_2 100]  
file     [log.h]        [open_dir_2 100]  
file     [test_gettimeofday.c]        [open_dir_2 100]  
file     [csdn.c]        [open_dir_2 100]  
file     [client.c]        [open_dir_2 100]  
file     [1.txt]        [open_dir_2 100]  
file     [test_opendir_readdir.c]        [open_dir_2 100]  
file     [curl-7.51.0.tar.gz]        [open_dir_2 100]  
file     [2.txt]        [open_dir_2 100]  
file     [opendir.c]        [open_dir_2 100]  
dir      [zlib-1.2.8]       [open_dir_2 96]  
file     [123.rmvb]        [open_dir_2 100]  
dir      [ffmpeg_learn]       [open_dir_2 96]  
file     [test_sem.c]        [open_dir_2 100]  
file     [test_list.c]        [open_dir_2 100]  
file     [list.h]        [open_dir_2 100]  
file     [server]        [open_dir_2 100]  
dir      [ProFFmpeg]       [open_dir_2 96]  
dir      [ffmpeg-3.1.6]       [open_dir_2 96]  
dir      [yasm-1.3.0]       [open_dir_2 96]  
file     [New0003.c]        [open_dir_2 100]  
dir      [.]       [open_dir_2 96]  
dir      [..]       [open_dir_2 96]  
file     [ffmpeg-3.1.6.tar.gz]        [open_dir_2 100]  
file     [types.h]        [open_dir_2 100]  
file     [New0002.c]        [open_dir_2 100]  
file     [simple.out]        [open_dir_2 100]  
file     [server.c]        [open_dir_2 100]  
dir      [learn]       [open_dir_2 96]  
file     [simple_ffmpeg_player.c]        [open_dir_2 100]  
file     [client]        [open_dir_2 100]  
dir      [abc]       [open_dir_2 96]  
file     [yasm-1.3.0.tar.gz]        [open_dir_2 100]  
file     [output.yuv]        [open_dir_2 100]  
file     [mySDLFirst.out]        [open_dir_2 100]  
-++--+++--[main 118]  
root@ubuntu:/share# 


你可能感兴趣的:(Unix高级环境编程)