linux dirent与stat

#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

char gInfoXmlPath[] = "/home/zgr/test/";

/**
 * 寻找符合过滤规则的文件
 */
int getNextXmlName_r(char* sFilter)
{
	DIR *dir = NULL;
	struct dirent entry;
	struct dirent *entryPtr = NULL;
	int retval = 0;
	struct stat entryInfo;
	char sXmlPathName[256];
	
	/* Open the given directory, if you can. */  
	dir = opendir(gInfoXmlPath);
	if( dir == NULL ) {
		return -1;
    }
	
	retval = readdir_r( dir, &entry, &entryPtr );
	while( retval==0 && entryPtr != NULL ) {
		if( ( strcmp( entry.d_name, ".") == 0 ) ||
		    ( strcmp( entry.d_name, "..") == 0 ) ) {
		    /* Short-circuit the . and .. entries. */
		    retval = readdir_r( dir, &entry, &entryPtr );
		    continue;
		}
		
		printf("*result:%s\n", entryPtr->d_name);
		
		if(NULL!=strstr(entry.d_name, sFilter)){
			sprintf(sXmlPathName, "%s%s", gInfoXmlPath, entry.d_name);
			if( lstat( sXmlPathName, &entryInfo ) == 0 ) {
				printf("name:%s st_mode:%u\n", entry.d_name, entryInfo.st_mode);
				printf("name:%s st_size:%ld\n", entry.d_name, entryInfo.st_size);
				printf("name:%s st_atime:%s\n", entry.d_name, ctime(&entryInfo.st_atime));
				printf("name:%s st_mtime:%s\n", entry.d_name, ctime(&entryInfo.st_mtime));
				printf("name:%s st_ctime:%s\n", entry.d_name, ctime(&entryInfo.st_ctime));
				printf("name:%s st_blksize:%ld\n", entry.d_name, entryInfo.st_blksize);
				printf("name:%s st_blocks:%ld\n", entry.d_name, entryInfo.st_blocks);
			}
		}
		
		retval = readdir_r( dir, &entry, &entryPtr );
	}
	closedir(dir);
	
	return 0;
}

int getNextXmlName(char* sFilter)
{
	DIR *dir = NULL;
	struct dirent *entry;
	struct stat entryInfo;
	char sXmlPathName[256];
	
	/* Open the given directory, if you can. */  
	dir = opendir(gInfoXmlPath);
	if( dir == NULL ) {
		return -1;
    }
	
	while( (entry = readdir(dir) ) != NULL ) {
		if( ( strcmp( entry->d_name, ".") == 0 ) ||
		    ( strcmp( entry->d_name, "..") == 0 ) ) {
		    /* Short-circuit the . and .. entries. */
		    continue;
		}
		
		if(NULL!=strstr(entry->d_name, sFilter)){
			sprintf(sXmlPathName, "%s%s", gInfoXmlPath, entry->d_name);
			if( lstat( sXmlPathName, &entryInfo ) == 0 ) {
				printf("name:%s st_mode:%u\n", entry->d_name, entryInfo.st_mode);
				printf("name:%s st_size:%ld\n", entry->d_name, entryInfo.st_size);
				printf("name:%s st_atime:%s\n", entry->d_name, ctime(&entryInfo.st_atime));
				printf("name:%s st_mtime:%s\n", entry->d_name, ctime(&entryInfo.st_mtime));
				printf("name:%s st_ctime:%s\n", entry->d_name, ctime(&entryInfo.st_ctime));
				printf("name:%s st_blksize:%ld\n", entry->d_name, entryInfo.st_blksize);
				printf("name:%s st_blocks:%ld\n", entry->d_name, entryInfo.st_blocks);
			}
		}
		
	}
	closedir(dir);
	
	return 0;
}

int main(int argc, char *argv[])
{
	char sFilter[32];
	strcpy(sFilter, "temp");
	getNextXmlName(sFilter);
	return 0;
}


1. DIR结构体,不用过多关注,在ubuntu10.04下没找到其定义。

不过在网上找到其面目如下:

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;  

2. struct dirent定义如下

struct dirent
  {
#ifndef __USE_FILE_OFFSET64
    __ino_t d_ino;
    __off_t d_off;
#else
    __ino64_t d_ino;
    __off64_t d_off;
#endif
    unsigned short int d_reclen;
    unsigned char d_type;
    char d_name[256];		/* We must not include limits.h! */
  };

3. 打开目录

DIR *opendir(const char *name); 
DIR *fdopendir(int fd);

4.  读目录

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

未完

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值