stat函数讲解_strcat函数

stat函数讲解_strcat函数stat函数讲解表头文件:   #include<sys/stat.h>            #include<unistd.h>定义函数:   intstat(constchar*file_name,structstat*buf);函数说明:  &nbs

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

stat函数讲解

表头文件: #include <sys/stat.h>
#include <unistd.h>
定义函数: int stat(const char *file_name, struct stat *buf);
函数说明: 通过文件名filename获取文件信息,并保存在buf所指的结构体stat中
返回值: 执行成功则返回0,失败返回-1,错误代码存于errno

错误代码:
ENOENT 参数file_name指定的文件不存在
ENOTDIR 路径中的目录存在但却非真正的目录
ELOOP 欲打开的文件有过多符号连接问题,上限为16符号连接
EFAULT 参数buf为无效指针,指向无法存在的内存空间
EACCESS 存取文件时被拒绝
ENOMEM 核心内存不足
ENAMETOOLONG 参数file_name的路径名称太长

#include <sys/stat.h>的作用

#include <sys/stat.h>

文件状态,
是unix/linux系统定义文件状态所在的伪标准头文件。
含有类型与函数:
dev_t st_dev Device ID of device containing file.
ino_t st_ino File serial number.
mode_t st_mode Mode of file (see below).
nlink_t st_nlink Number of hard links to the file.
uid_t st_uid User ID of file.
gid_t st_gid Group ID of file.
dev_t st_rdev Device ID (if file is character or block special).
off_t st_size For regular files, the file size in bytes.
For symbolic links, the length in bytes of the
pathname contained in the symbolic link.

                               For a shared memory object, the length in bytes.

                               For a typed memory object, the length in bytes.

                               For other file types, the use of this field is
                               unspecified.
          time_t    st_atime   Time of last access.
          time_t    st_mtime   Time of last data modification.
          time_t    st_ctime   Time of last status change.
          int    chmod(const char *, mode_t);

          int    fchmod(int, mode_t);
          int    fstat(int, struct stat *);
          int    lstat(const char *restrict, struct stat *restrict);
          int    mkdir(const char *, mode_t);
          int    mkfifo(const char *, mode_t);
          int    mknod(const char *, mode_t, dev_t);
          int    stat(const char *restrict, struct stat *restrict);
          mode_t umask(mode_t);

使用stat函数最多的可能是ls-l命令,用其可以获得有关一个文件的所有信息。
一般头文件在/usr/include下面,这里是标准C程序头文件,如果你的头文件前加了 <sys/*>,那说明这是系统调用函数头文件,其在/usr/include/sys下面。

函数都是获取文件(普通文件,目录,管道,socket,字符,块()的属性。函数原型#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);连接文件描述命,获取文件属性。
文件对应的属性struct stat {

mode_t st_mode; //文件对应的模式,文件,目录等
ino_t st_ino; //inode节点号
dev_t st_dev; //设备号码
dev_t st_rdev; //特殊设备号码
nlink_t st_nlink; //文件的连接数
uid_t st_uid; //文件所有者
gid_t st_gid; //文件所有者对应的组
off_t st_size; //普通文件,对应的文件字节数
time_t st_atime; //文件最后被访问的时间
time_t st_mtime; //文件内容最后被修改的时间
time_t st_ctime; //文件状态改变时间
blksize_t st_blksize; //文件内容对应的块大小
blkcnt_t st_blocks; //伟建内容对应的块数量
};
示例:
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

int main() {

struct stat buf;
stat(“/etc/hosts”, &buf);
printf(“/etc/hosts file size = %d\n”, buf.st_size);
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/171784.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • unity 的Cinemachine组件运用

    unity 的Cinemachine组件运用1.第三人称视角控制通过PackageManager安装CineMachine1) 最简单的方法使用freeLook虚拟相机常用的调整为:1.观察目标:将要看的目标放在这里。2输入控制:把你想用来控制的虚拟轴(就是InputManager里的)的名字输入进去就行。默认是填mouse那个输入轴。注意:似乎不支持NewInputSystem。所以在用NewInputSystem时要么用在projectSetting/player里改成both设置。要么自己写脚本去调用这个组件中的

    2022年5月12日
    102
  • html下划线[通俗易懂]

    html下划线[通俗易懂]<spanstyle=”text-decoration:underline;”> 划重点</span>

    2022年6月4日
    23
  • Java虚拟机:class类文件结构

    Java虚拟机:class类文件结构

    2021年9月26日
    37
  • Material Design 实战 之第五弹 —— 下拉刷新(SwipeRefreshLayout) …

    Material Design 实战 之第五弹 —— 下拉刷新(SwipeRefreshLayout) …

    2021年6月14日
    82
  • pycharm安装教程2020.3.4_pycharm安装后无解释器

    pycharm安装教程2020.3.4_pycharm安装后无解释器第一步安装解释器,第二步安装pycharm1第一步安装解释器1.1什么是解释器:??就是将Python程序翻译成为计算机可以识别的01代码1.2安装解释器:解释器安装地址:https://www.python.org/downloads/release/python-372根据自己的操作系统安装适配的解释器:这里以Windows为例注意安装的时候我们需要需注意吧解释器添加到环境变量里面双击开始安装勾选addpythontopath,如果安装的时候没有勾选,请安装结束以后按

    2022年8月26日
    2
  • Windows系统装机步骤以及常见专用名词汇总(实用)

    Windows系统装机步骤以及常见专用名词汇总(实用)一、装机流程(U盘安装或重装系统)注意:各个型号品牌的操作可能不同,但总体流程基本相同,实操注意查漏补缺。1.安装好主机(CPU,硬盘,内存,散热器,主板,电源,机箱,(显卡或者网卡使用的PCIE接口)),合上机箱。2.准备工作:将电源线(显示器、主机)插好,连接好显示器和主机之间的VGA线,插好键鼠的USB线。3.准备好一个启动U盘(百度“优启通”)。4.插入启动U盘并开启主机(按下电源键),根据电脑自己的装机BIOS快捷键(例如…

    2022年6月25日
    23

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号