int stat(const char *path, struct stat *buf);

int fstat(int fd, struct stat *buf);

唯一不同是参数不同,其他一样。

文件的一些属性参数:

struct stat{

  dev_t st_dev; /* ID of device containing file */
  ino_t st_ino; /* inode number */ 文件inode号
  mode_t st_mode; /* protection */ 
  nlink_t st_nlink; /* number of hard links */ 硬链接数
  uid_t st_uid; /* user ID of owner */  文件的UID
  gid_t st_gid; /* group ID of owner */文件的GID
  dev_t st_rdev; /* device ID (if special file) */设备ID(如果特殊的文件)
  off_t st_size; /* total size, in bytes */文件大小
  blksize_t st_blksize; /* blocksize for filesystem I/O */
  blkcnt_t st_blocks; /* number of 512B blocks allocated */
  time_t st_atime; /* time of last access */ 文件的三个时间atime mtime atime
  time_t st_mtime; /* time of last modification */
  time_t st_ctime; /* time of last status change */
};

 通过下面的一些宏函数定义可以判断文件类型:

The following POSIX macros are defined to check the file type using the st_mode field:

下面的POSIX宏定义使用st_mode字段检查文件类型:

如果如下宏返回真就说明是所指文件

if( S_ISREG(st_mode) )

{

说明是普通文件

}

S_ISREG(m) is it a regular file? 普通文件

S_ISDIR(m) directory?目录

S_ISCHR(m) character device? 字符设备

S_ISBLK(m) block device?块设备

S_ISFIFO(m) FIFO (named pipe)? 管道

S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.) 符号链接

S_ISSOCK(m) socket? (Not in POSIX.1-1996.)套接字

例子:

int main(int argc , char* argv[])
{
  struct stat fileinfo;
  int fd;
  if(argc > 2 ) return 0;

  fd = open(argv[1] , O_RDONLY);

  if(fd == -1)
  {
    printf("%s\n" , strerror(errno));
  }
  else
  {
    fstat(fd , &fileinfo);//获取文件属性 //stat只需要改下参数即可

    if( S_ISREG(fileinfo.st_mode) )
    {
      printf("普通文件\n");
    }
  }
  close(fd);

  return 0;

}

fstat().stat()函数的更多相关文章

  1. linux下实现ls()函数遍历目录

    转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600794.html 需求:在linux下遍历目录,输出目录中各文件名. 在linux下遍历目录的相关函数有 ...

  2. [C++]linux下实现ls()函数遍历目录

    转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600794.html 需求:在linux下遍历目录,输出目录中各文件名. 在linux下遍历目录的相关函数有 ...

  3. ext2文件系统

    2.1. 总体存储布局 我们知道,一个磁盘可以划分成多个分区,每个分区必须先用格式化工具(例如某种mkfs命令)格式化成某种格式的文件系统,然后才能存储文件,格式化的过程会在磁盘上写一些管理存储布局的 ...

  4. Linux系统编程(6)——文件系统

    计算机的文件系统是一种存储和组织计算机数据的方法,它使得对其访问和查找变得容易,文件系统使用文件和树形目录的抽象逻辑概念代替了硬盘和光盘等物理设备使用数据块的概念,用户使用文件系统来保存数据不必关心数 ...

  5. inode表元数据,存储在物理存储体上

    一个文件除了数据需要存储之外,一些描述信息也需要存储,例如文件类型(常规.目录.符号链接等),权限,文件大小,创建/修改/访问时间等,也就是ls -l命令看到的那些信息,这些信息存在inode中而不是 ...

  6. Node.js:文件系统

    ylbtech-Node.js:文件系统 1.返回顶部 1. Node.js 文件系统 Node.js 提供一组类似 UNIX(POSIX)标准的文件操作API. Node 导入文件系统模块(fs)语 ...

  7. 33.Node.js 文件系统fs

    转自:http://www.runoob.com/nodejs/nodejs-module-system.html Node.js 提供一组类似 UNIX(POSIX)标准的文件操作API. Node ...

  8. Node.js Learning Notes

    简介 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O服务 ...

  9. C语言:stat,fstat和lstat函数

    这三个函数的功能是一致的,都用于获取文件相关信息,但应用于不同的文件对象.对于函数中给出pathname参数,stat函数返回与此命名文件有关的信息结构,fstat函数获取已在描述符fields上打开 ...

随机推荐

  1. Identity-第三章 Authorize原理解析

    本篇旨在解析Identity中角色限制的原理. 需要的工具:Visual Studio.Reflector 问题提出: 1.当我们需要限制某个Controller的名称只需要某个或者某几个角色访问,这 ...

  2. RSA加密解密操作

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  3. 理解C# Attribute

    1.Attribute与Property Attribute是特性,Property是属性. 2.Attribute与注释 注释:是给程序员看的,编译的时候会去掉这些信息,也就是说,程序集中没有注释的 ...

  4. Ubuntu 12.04 升级到14.04之后,pidgin-sipe 出现的问题: Trouble with the pidgin and self-signed SSL certificate

    Once again, I run into trouble when upgrading my LinuxMint. In last few days, my Linux mint notifies ...

  5. preference activity框架

    从android3.0开始preference框架做了重大改变 框架由一下四部分组成 1.preference screen布局 一个xml文件,指定了要显示的Preference控件. 每个控件应当 ...

  6. android照片墙的实现

    转载自 http://blog.csdn.net/guolin_blog/article/details/9526203 由于每个android应用程序都有内存限制,所以如果加载很多图片的话,一定会出 ...

  7. [Webpack 2] Import a non-ES6 module with Webpack

    When you have a dependency that does not export itself properly, you can use the exports-loader to f ...

  8. Systemd 入门教程:命令篇

    http://www.ruanyifeng.com/blog/2014/09/illustration-ssl.html

  9. insert例子

    11.20 使用insert代替下标操作. #include<iostream> #include<map> #include<string> #include&l ...

  10. linux如何查进程、杀进程

    本文系转载,转载原文地址:http://blog.sina.com.cn/s/blog_637112040100vl53.html 1.查进程   ps命令查找与进程相关的PID号:   ps a 显 ...