linux dirent与stat

来源:互联网 发布:java语言基础教程 编辑:程序博客网 时间:2024/06/15 18:27
#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);

未完

0 0
原创粉丝点击