Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1705037
  • 博文数量: 263
  • 博客积分: 1218
  • 博客等级: 少尉
  • 技术积分: 2862
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-19 02:33
文章分类

全部博文(263)

文章存档

2020年(12)

2019年(2)

2018年(10)

2016年(1)

2015年(20)

2014年(115)

2013年(46)

2012年(37)

2011年(20)

分类: C/C++

2014-08-18 13:45:20


在Linux中,可以利用stat()函数来获取一个文件的状态

  1. #include <sys/stat.h>
  2. #include <unistd.h>
  3.   
  4. int stat(const char *file_name, struct stat *buf);
这个函数执行成功返回0,失败返回-1。取得的文件状态存放在buf指针指向的struct stat结构提中 .
转::http://blog.chinaunix.net/uid-25958655-id-4365384.html
原文: http://blog.csdn.net/simmerlee/article/details/8281399


在不同的linux和unix中, struct stat具体声明时,里面的成员顺序不一定相同与中说的一模一样.
让我们去查找一下, 在/usr/include/sys/stat.h中没找到struct stat 定义, 只能去它所包含的头文中去找,它包含了这几个头文件,#include,#include,#include,#include
RedHat linux6.4 中(其它RedHat和Fedora估计类似),struct stat 的定义是在/usr/include/bits/stat.h 头文件中声明形式如下:
  1. struct stat
  2.       {
  3.         __dev_t st_dev; /* Device. */
  4.         unsigned short int __pad1;
  5.     #ifndef __USE_FILE_OFFSET64
  6.         __ino_t st_ino; /* File serial number. */
  7.     #else
  8.         __ino_t __st_ino; /* 32bit file serial number. */
  9.     #endif
  10.         __mode_t st_mode; /* File mode. */
  11.         __nlink_t st_nlink; /* Link count. */
  12.         __uid_t st_uid; /* User ID of the file's owner. */
  13.         __gid_t st_gid; /* Group ID of the file's group.*/
  14.         __dev_t st_rdev; /* Device number, if device. */
  15.         unsigned short int __pad2;
  16.     #ifndef __USE_FILE_OFFSET64
  17.         __off_t st_size; /* Size of file, in bytes. */
  18.     #else
  19.         __off64_t st_size; /* Size of file, in bytes. */
  20.     #endif
  21.         __blksize_t st_blksize; /* Optimal block size for I/O. */
  22.     
  23.     #ifndef __USE_FILE_OFFSET64
  24.         __blkcnt_t st_blocks; /* Number 512-byte blocks allocated. */
  25.     #else
  26.         __blkcnt64_t st_blocks; /* Number 512-byte blocks allocated. */
  27.     #endif
  28.     #if defined __USE_MISC || defined __USE_XOPEN2K8
  29.         /* Nanosecond resolution timestamps are stored in a format
  30.            equivalent to 'struct timespec'. This is the type used
  31.            whenever possible but the Unix namespace rules do not allow the
  32.            identifier 'timespec' to appear in the <sys/stat.h> header.
  33.            Therefore we have to handle the use of this header in strictly
  34.            standard-compliant sources special. */
  35.         struct timespec st_atim;   /* Time of last access. */
  36.         struct timespec st_mtim;   /* Time of last modification. */
  37.         struct timespec st_ctim;   /* Time of last status change. */
  38.     # define st_atime st_atim.tv_sec /* Backward compatibility. */
  39.     # define st_mtime st_mtim.tv_sec
  40.     # define st_ctime st_ctim.tv_sec
  41.     #else
  42.         __time_t st_atime; /* Time of last access. */
  43.         unsigned long int st_atimensec; /* Nscecs of last access. */
  44.         __time_t st_mtime; /* Time of last modification. */
  45.         unsigned long int st_mtimensec; /* Nsecs of last modification. */
  46.         __time_t st_ctime; /* Time of last status change. */
  47.         unsigned long int st_ctimensec; /* Nsecs of last status change. */
  48.     #endif
  49.     #ifndef __USE_FILE_OFFSET64
  50.         unsigned long int __unused4;
  51.         unsigned long int __unused5;
  52.     #else
  53.         __ino64_t st_ino; /* File serial number. */
  54.     #endif

  55. };





而mode_t呢, 从 "usr/include/sys/stat.h"中的 一行"#include     /* For __mode_t and __dev_t.  */ " 就可以知道__mode_t应该是在/usr/include/sys/bits/types.h中

当然如果你只是想知道 stat中每一种成员类型占用了几个字节的话, 只要sizeof(类型)就好拉, 如printf("%d",sizeof(dev_t));

还有这个另外一篇: http://blog.chinaunix.net/uid-25958655-id-4365384.html
阅读(3514) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~