linux C之stat()
2016-07-12 08:37
363 查看
头文件:
#include <sys/stat.h>
#include <unistd.h>
定义函数:
int stat(const char *file_name, struct stat *buf);
函数说明:
通过文件名filename获取文件信息,并保存在buf所指的结构体stat中
返回值:
执行成功则返回0,失败返回-1,错误代码存于errno
errno:
ENOENT 参数file_name指定的文件不存在
ENOTDIR 路径中的目录存在但却非真正的目录
ELOOP 欲打开的文件有过多符号连接问题,上限为16符号连接
EFAULT 参数buf为无效指针,指向无法存在的内存空间
EACCESS 存取文件时被拒绝
ENOMEM 核心内存不足
ENAMETOOLONG 参数file_name的路径名称太长
[b]范例:[/b]
[cpp]
view plain
copy
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
int main() {
struct stat buf;
stat("test", &buf);
printf("the file size = %d\n", buf.st_size);
}
stat结构体:
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; //最后一次改变时间(指属性)
};
#include <sys/stat.h>
#include <unistd.h>
定义函数:
int stat(const char *file_name, struct stat *buf);
函数说明:
通过文件名filename获取文件信息,并保存在buf所指的结构体stat中
返回值:
执行成功则返回0,失败返回-1,错误代码存于errno
errno:
ENOENT 参数file_name指定的文件不存在
ENOTDIR 路径中的目录存在但却非真正的目录
ELOOP 欲打开的文件有过多符号连接问题,上限为16符号连接
EFAULT 参数buf为无效指针,指向无法存在的内存空间
EACCESS 存取文件时被拒绝
ENOMEM 核心内存不足
ENAMETOOLONG 参数file_name的路径名称太长
[b]范例:[/b]
[cpp]
view plain
copy
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
int main() {
struct stat buf;
stat("test", &buf);
printf("the file size = %d\n", buf.st_size);
}
stat结构体:
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; //最后一次改变时间(指属性)
};
相关文章推荐
- 关闭 linux下的umask( )函数、setsid( )函数
- CenOS 7 安装配置Samba服务
- linux系统移植和根文件系统制作
- linuxvi快捷键
- linux升级python,安装pip等。
- 如何快速查看linux的发行版信息
- linux系统的crond服务
- 学习LINUX运维决心书
- linux修改host文件
- 如何开启Centos6.4系统的SSH服务
- linux expect详解(ssh自动登录)
- Linux用户和用户组
- 初学者指南:在 Ubuntu Linux 上安装和使用 Git 和 GitHub
- Linux再三小心操作的9个命令
- AES加密在linux下不能正常使用
- linux awk命令
- linux整理
- [Linux] 流 ( Stream )、管道 ( Pipeline ) 、Filter - 笔记
- Samba实现Linux与Window文件的传输
- Linux_更改yum源