linux C函数之stat函数
来源:互联网 发布:西班牙运营商网络频段 编辑:程序博客网 时间:2024/06/05 15:05
linuxC函数之stat函数
1.函数功能:
通过文件名filename获取文件信息,并保存在buf所指的结构体stat中
2.函数原型
1)函数头文件
- #include <sys/stat.h>
- #include <unistd.h>
2)函数
- int stat(const char *file_name, struct stat *buf )
3)返回返回值:
执行成功则返回0,失败返回-1,错误代码存于errno
错误代码:
ENOENT 参数file_name指定的文件不存在
ENOTDIR 路径中的目录存在但却非真正的目
ELOOP 欲打开的文件有过多符号连接问题,上限为16符号连接
EFAULT 参数buf为无效指针,指向无法存在的内存空间
EACCESS 存取文件时被拒绝
ENOMEM 核心内存不足
ENAMETOOLONG 参数file_name的路径名称太长数值
4)相关结构体
- struct stat {
- dev_t st_dev; //文件的设备编号
- ino_t st_ino; //节点
- mode_t st_mode; //文件的类型和存取的权限
- nlink_t st_nlink; //连到该文件的硬连接数目,刚建立的文件值为1
- uid_t st_uid; //用户ID
- gid_t st_gid; //组ID
- dev_t st_rdev; //(设备类型)若此文件为设备文件,则为其设备编号
- off_t st_size; //文件字节数(文件大小)
- unsigned long st_blksize; //块大小(文件系统的I/O 缓冲区大小)
- unsigned long st_blocks; //块数
- time_t st_atime; //最后一次访问时间
- time_t st_mtime; //最后一次修改时间
- time_t st_ctime; //最后一次改变时间(指属性)
- };
st_mode
- S_IFMT 0170000 文件类型的位遮罩
- S_IFSOCK 0140000 scoket
- S_IFLNK 0120000 符号连接
- S_IFREG 0100000 一般文件
- S_IFBLK 0060000 区块装置
- S_IFDIR 0040000 目录
- S_IFCHR 0020000 字符装置
- S_IFIFO 0010000 先进先出
- S_ISUID 04000 文件的(set user-id on execution)位
- S_ISGID 02000 文件的(set group-id on execution)位
- S_ISVTX 01000 文件的sticky位
- S_IRUSR(S_IREAD) 00400 文件所有者具可读取权限
- S_IWUSR(S_IWRITE)00200 文件所有者具可写入权限
- S_IXUSR(S_IEXEC) 00100 文件所有者具可执行权限
- S_IRGRP 00040 用户组具可读取权限
- S_IWGRP 00020 用户组具可写入权限
- S_IXGRP 00010 用户组具可执行权限
- S_IROTH 00004 其他用户具可读取权限
- S_IWOTH 00002 其他用户具可写入权限
- S_IXOTH 00001 其他用户具可执行权限
在Linux中定义了检查这些类型的宏定义
- S_ISLNK (st_mode) 判断是否为符号连接
- S_ISREG (st_mode) 是否为一般文件
- S_ISDIR (st_mode) 是否为目录
- S_ISCHR (st_mode) 是否为字符三设备文件
- S_ISBLK (s3e) 是否为先进先出
- S_ISSOCK (st_mode) 是否为socket
3.stat,fstat,lstat函数的区别和相同点
1)原型
- #include<sys/types.h>
- #include<sys/stat.h>
- int stat(const char *restrict pathname, struct stat *restrict buf);
- int fstat(int filedes, struct stat *buf);
- int lstat(const char *restrict pathname, struct stat *restrict buf);
三个函数的返回值,若成功则为0,若出错则为-1。给定一个pathname,stat函数返回一个与此命名文件有关的信息结构,fstat函数获得已在描述符filedes上打开的文件的有关信息。lstat函数类似于stat,但是当命名的文件是一个符号连接时,lstat返回该符号连接的有关信息,而不是由该符号连接引用的文件的信息。
0 0
- linux C函数之stat函数
- linux C函数之stat函数
- linux C函数之stat函数
- linux之stat函数解析
- linux编程之stat函数
- C中的stat()函数
- linux stat函数讲解
- linux stat函数讲解
- Linux stat函数讲解
- Linux stat函数讲解
- Linux stat函数讲解
- linux stat函数讲解
- Linux stat函数讲解
- Linux stat函数讲解
- linux stat函数讲解
- linux stat 函数讲解
- linux stat函数讲解
- Linux stat函数讲解
- HTTP Digest authentication
- JAVA 类集
- 快速排序quick sort
- POJ - 3342 Party at Hali-Bula 树形DP
- java-I/O File类(5)-Reader和Writer、OutputStreamWriter 、BufferedWriter、字节流和字符流的区别
- linux C函数之stat函数
- Collections sort
- wordlist 1 - page 6
- pyqt实现窗口边框阴影效果(python)
- c++学习第一课--输入/输出
- Java高新技术:代理
- ubuntu(linux)下谷歌浏览器跨域问题
- hdu 1045 Fire Net(二分匹配)
- More Effictive C++读书笔记