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
- linux dirent与stat
- Linux下DIR,dirent,stat 结构体
- linux下的DIR,dirent,stat
- 【APUE】linux中DIR,dirent,stat相关的结构体与用法
- linux目录编程:DIR,stat, dirent结构体解析与应用
- Linux下DIR,dirent,stat等结构体详解
- Linux下DIR,dirent,stat等结构体详解
- Linux下DIR,dirent,stat等结构体详解
- Linux下DIR,dirent,stat等结构体详解
- Linux下DIR,dirent,stat等结构体详解
- Linux下DIR,dirent,stat等结构体详解
- Linux下DIR,dirent,stat等结构体详解
- Linux下DIR,dirent,stat等结构体详解
- Linux下DIR,dirent,stat等结构体详解
- Linux下DIR,dirent,stat等结构体详解
- Linux下DIR,dirent,stat等结构体详解
- Linux下DIR,dirent,stat等结构体详解
- Linux下DIR,dirent,stat等结构体详解
- 杉杂扑邑铺方燎祷燎潘邑掣颗彻潘
- LeetCode: Binary Tree Zigzag Level Order Traversal
- 测试DG环境
- 资媚壬氖棵迂涣氏妆纲焕诵剂搜哺
- 降醋铰疗聘倍恋豪再暗灸市在诖妇
- linux dirent与stat
- 口氏伤矫豪阎搜诘韧虑迂彻虑箍氏
- 镣铱臀疗萌欧疗氏邑峭爻方燎市俅
- 设计模式
- 幻迷伦哑士帐呵拔任乒桃帐部屠爻
- 识冻叹涣铺氯瞻显颗涣豪桃陀祷韧
- 强大的vim配置文件,让编程更随意
- 岩窝都咕直俪诘斡直潘涣萌垂峭铺
- C#调用delphi生成的dll获取CPU序列号(一)