Header file:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

DEFINITION:

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

DESCRIPTION:

  The functions return information about a file, in the buffer pointed to by buf.

No permissions are required on the file itself, but—in the case of stat(), fstatat(), and
lstat()—execute (search) permission is required on all of the directories in pathname
that lead to the file.

stat() and fstatat() retrieve information about the file pointed to by pathname;

stat() and fstatat() retrieve information about the file pointed to by pathname; the differences for fstatat() are described below.

lstat() is identical to stat(), except that if pathname is a symbolic link, then it returns information about the link itself, not the file that it refers to.

fstat() is identical to stat(), except that the file about which information is to be retrieved is specified by the file descriptor fd.

RETURN VALUE:

On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.

ERRORS:

EACCES Search permission is denied for one of the directories in the path prefix of path‐name. (See also path_resolution(7).)

EBADF fd is bad.

EFAULT Bad address.

ELOOP Too many symbolic links encountered while traversing the path.

ENAMETOOLONG   pathname is too long.

ENOENT       A component of pathname does not exist, or pathname is an empty string.

ENOMEM   Out of memory (i.e., kernel memory).

ENOTDIR  A component of the path prefix of pathname is not a directory.

EOVERFLOW  pathname or fd refers to a file whose size, inode number, or number of blocks can‐not be represented in, respectively, the types off_t, ino_t, or blkcnt_t. This
error can occur when, for example, an application compiled on a 32-bit platform without -D_FILE_OFFSET_BITS=64 calls stat() on a file whose size exceeds (1<<31)-1 bytes.

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h> int main() {
struct stat buf;
stat("HOME/C++/test", &buf);
printf("the file size = %d\n", buf.st_size);
}

stat structure:

           struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
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 */ /* Since Linux 2.6, the kernel supports nanosecond
precision for the following timestamp fields.
For the details before Linux 2.6, see NOTES. */

      

          struct timespec st_atim; /* time of last access */
          struct timespec st_mtim; /* time of last modification */
          struct timespec st_ctim; /* time of last status change */

          #define st_atime st_atim.tv_sec /* Backward compatibility */
          #define st_mtime st_mtim.tv_sec
          #define st_ctime st_ctim.tv_sec};

};

fstat(int fd,struct stat *)接收的已open的文件描述符

stat(char *filename,struct stat *)接收的路径名, 需要注意的是 能处理符号链接,但处理的是符号链接指向的文件。

lstat(char *filename,struct stat *)接收的路径名  ,需要注意的是,也能能处理符号链接,但处理的是符号链接本身(自身)文件。

Linux 基础(一)stat函数的更多相关文章

  1. Linux基础(五) Shell函数

    Shell 函数 linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action ...

  2. 原来今天是感恩节-Linux基础继续&MySQL和PHP

    hi 原来今天是感恩节.虽然一直没有过这个节日的习惯,但仅仅是听到感恩的消息,都能想到一幅幅画面.愿大家安好! 下午开题会议还是有所收获,悄悄的,就变向那个不喜欢自己的人了. 一.Linux基础(二) ...

  3. 买错的电影票,含着泪也得看-LAMP搭建&Linux基础

    hi 没说过,上周五室友过生请客,在龙湖里吃嗨了喝爽了,回去的路上侃侃而谈.说好的这周一起去看年内最后的大片,火星救援的,谁知道老子眼神不好,买错了电影的时间...把周六的约定提前到了今儿个下午,ma ...

  4. Linux基础入门

    第一节,linux系统简介 一.实验内容 了解 Linux 的历史,Linux 与 Windows 的区别等入门知识. 二.实验要求 阅读linux简介与历史 三.实验步骤 (一).Linux 为何物 ...

  5. Linux基础与Linux下C语言编程基础

    Linux基础 1 Linux命令 如果使用GUI,Linux和Windows没有什么区别.Linux学习应用的一个特点是通过命令行进行使用. 登录Linux后,我们就可以在#或$符后面去输入命令,有 ...

  6. Linux基础入门学习笔记20135227黄晓妍

    学习计时:共24小时 读书:1小时 代码:8小时 作业:3小时 博客:12小时 一.学习目标 1. 能够独立安装Linux操作系统   2. 能够熟练使用Linux系统的基本命令   3. 熟练使用L ...

  7. Linux基础01 学会使用命令帮助

    Linux基础01 学会使用命令帮助 概述 在linux终端,面对命令不知道怎么用,或不记得命令的拼写及参数时,我们需要求助于系统的帮助文档:linux系统内置的帮助文档很详细,通常能解决我们的问题, ...

  8. linux基础之Shell Script入门介绍

    本文介绍下,学习shell script编程的入门知识,通过几个入门实例,带领大家走进shell script的神圣殿堂,呵呵,有需要的朋友参考下. 本文转自:http://www.jbxue.com ...

  9. 【Python之路】第一篇--Linux基础命令

    pwd 命令 查看”当前工作目录“的完整路径 pwd -P # 显示出实际路径,而非使用连接(link)路径:pwd显示的是连接路径 .   表示当前目录 ..  表示上级目录 /  表示根目录 ls ...

  10. Linux基础(6)

    Linux基础(六) shell脚本中的三大循环和函数知识点 一.流程控制之if结构 1.简单的if实例: #!/bin/bash var='/etc/init.d' #var='/dev/sda' ...

随机推荐

  1. 如何用Excel打开CSV文件

    如何用Excel打开CSV文件? CSV文件一般是MS-SQL 导出查询数据的一种格式.格式结构是 用逗号分隔数据,如果直接用Excel打开那么数据不会自动分列.需要进行一定的设置.下面是设置过程. ...

  2. 【JZOJ4782】【NOIP2016提高A组模拟9.15】Math

    题目描述 输入 输出 样例输入 3 5 样例输出 -1 数据范围 解法 观察式子,可以得知整个式子与d(i*j)的奇偶性有关. d(n)为奇数当且仅当n是完全平方数. 对于一个i,如果d(i*j) ( ...

  3. python流程控制和循环

    变量的命名:可以由数字字母下换线组成 ,不能以数字开头,可以使用中文但是不推荐使用中文,不推荐前面使用_ __,不能使用系统的关键字,变量名严格区分大小写 逻辑运算优先级 or<and<n ...

  4. docker--docker基本命令使用及发布镜像

    docker镜像可以完全看作一台全新的电脑使用,无论什么镜像都是对某一东西进行了配置,然后打包后可以快速移植到需要的地方直接使用 省去复杂的配置工作 比如java web项目部署,如果是新部署,需要装 ...

  5. MySQL非安装版安装

    1 数据库的打开方式(非安装版本) 1.解压mysql-5.7.12-winx64.zip到一个路径上没有空格没有汉字的目录中 2.复制my-default.ini重命名为my.ini 3.命令行进入 ...

  6. iOS 9 新特性之实现 3D Touch

    http://www.cocoachina.com/ios/20151027/13812.html 10月19号,周末,起床去吃早餐,吃完回来顺便去沃尔玛逛逛,把晚上的菜给买了,逛着逛着就来到了卖苹果 ...

  7. Python学习之路4☞条件和循环

    一.if语句 1.1 功能 计算机又被称作电脑,意指计算机可以像人脑一样,根据周围环境条件(即expession)的变化做出不同的反应(即执行代码) if语句就是来控制计算机实现这一功能 1.2 语法 ...

  8. WebLogic Server再曝高风险远程命令执行0day漏洞,阿里云WAF支持免费应急服务

    6月11日,阿里云安全团队发现WebLogic CVE-2019-2725补丁绕过的0day漏洞,并第一时间上报Oracle官方, 6月12日获得Oracle官方确认.由于Oracle尚未发布官方补丁 ...

  9. @loj - 2093@ 「ZJOI2016」线段树

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 小 Yuuka 遇到了一个题目:有一个序列 a1,a2,..., ...

  10. LeetCode63 Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...