stat():用于获取文件的状态信息,使用时需要包含<sys/stat.h>头文件。

函数原型:int stat(const char *path, struct stat *buf);

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 file system I/O */
  blkcnt_t st_blocks;     /* number of 512B blocks allocated */
  time_t st_atime;      /* time of last access */
  time_t st_mtime;     /* time of last modification */
  time_t st_ctime;      /* time of last status change */
};

示例:

int main(int argc, char* argv[])
{
struct stat buf;
char* path = "E:\\1.txt"; int res = stat(path, &buf);
if (res != )
{
perror("Problem getting information");
switch (errno)
{
case ENOENT:
printf("File %s not found.\n", path);
break;
case EINVAL:
printf("Invalid parameter to _stat.\n");
break;
default:
/* Should never be reached. */
printf("Unexpected error in _stat.\n");
}
} //获取文件类型
if (buf.st_mode & S_IFREG)
printf("regular file\n");
else if (buf.st_mode & S_IFDIR)
printf("dir \n");
else
printf("other\n"); //获取大小
printf("SIZE %d\n", buf.st_size); char timeBuf[] = { };
printf("%lld\n", buf.st_ctime);
//文件最后访问时间
errno_t err = ctime_s(timeBuf, ,&buf.st_atime);
if (err)
{
printf("Invalid arguments to ctime_s.");
return -;
}
printf("Time visit %s\n", timeBuf); //文件最后修改时间
err = ctime_s(timeBuf, , &buf.st_mtime);
if(err)
{
printf("Invalid arguments to ctime_s.");
return -;
}
printf("Time modified %s\n", timeBuf); system("pause");
return ;
}

示例及参考来源:https://docs.microsoft.com/zh-cn/cpp/c-runtime-library/reference/stat-functions

stat()函数--------------获取文件信息的更多相关文章

  1. 第九篇:使用 lstat 函数获取文件信息

    前言 在之前的文章中,描述过如何用 fcntl 函数改变文件的状态标记.但,文件还有很多信息,如文件类型,权限设置,设备编号,访问时间等等.如果要获取这些信息,则使用函数 lstat 可以轻松达到这个 ...

  2. 使用 lstat 函数获取文件信息

    前言 在之前的文章中,描述过如何用 fcntl 函数改变文件的状态标记.但,文件还有很多信息,如文件类型,权限设置,设备编号,访问时间等等.如果要获取这些信息,则使用函数 lstat 可以轻松达到这个 ...

  3. 【转】linux C++ 获取文件信息 stat函数详解

    stat函数讲解 表头文件:    #include <sys/stat.h>             #include <unistd.h>定义函数:    int stat ...

  4. 使用QFileInfo类获取文件信息(在NTFS文件系统上,出于性能考虑,文件的所有权和权限检查在默认情况下是被禁用的,通过qt_ntfs_permission_lookup开启和操作。absolutePath()必须查询文件系统。而path()函数,可以直接作用于文件名本身,所以,path() 函数的运行会更快)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Amnes1a/article/details/65444966QFileInfo类为我们提供了系统无 ...

  5. 使用QFileInfo类获取文件信息(文件的所有权和权限检查在默认情况下是被禁用的。要使能这个功能 extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;)

    QFileInfo类为我们提供了系统无关的文件信息,包括文件的名字和在文件系统中位置,文件的访问权限,是否是目录或符合链接,等等.并且,通过这个类,可以修改文件的大小和最后修改.读取时间.同时,QFi ...

  6. FastDFS 通过文件名获取文件信息

    /** * 获取文件信息 * * param string group_name 文件所在的组名 * param string file_id 文件id(如: M00/09/BE/rBBZolgj6O ...

  7. Unix无缓冲文件操作函数、文件信息查询

    问题描述:         Unix无缓冲文件操作函数.文件信息查询 问题解决:        struct stat 结构体信息: 具体代码: 具体源文件:

  8. C# -- 使用FileInfo获取文件信息

    C# -- 使用FileInfo获取文件信息 1. 代码实现 static void Main(string[] args) { GetFileInfo(@"D:\Test.xlsx&quo ...

  9. C#中获取文件信息的代码

    如下的内容内容是关于C#中获取文件信息的内容,应该对大伙有一些好处. FileInfo fi = new FileInfo(@"C:file.txt"); if(fi.Exists ...

随机推荐

  1. 关于shopee平台接口(php)对接示例

    2018年8月之后,shopee开始使用新接口,需要进行授权操作 1.授权 public function getAuth(){ /** * @param ShopApiShopee $model * ...

  2. Python笔记_第二篇_面向过程_第二部分_3.模块的概述

    这部分内容是非常重要的,分模块的基本概念和一些常用模块的使用,其实常用模块使用这部分也不是太全面,后续或者有机会再通过其他材料进行讲解. 1. 模块的概述: 目前代码比较少,写在一个文件中还体现不出什 ...

  3. 对《The future of ReactiveCocoa》的一些思考

    前言 我以为 第一次接触 swift 语言时,看到函数的表示形式如下: func fun(num: Int) -> Int { return num + 1 } let f = fun(1) 和 ...

  4. docker安装(centos-7)

    centos7安装docker:Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker .通过 uname -r 命令 ...

  5. [LC] 1048. Longest String Chain

    Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predece ...

  6. [LC] 146. LRU Cache

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  7. spring boot web 开发及数据库操作

    推荐网站http://springboot.fun/ 1.json 接口开发 2.自定义 filter 3.自定义 property 4.log 配置 5.数据库操作 6.测试

  8. Linux命令:ldd

    1.ldd不是一个可执行程序,而是一个shell脚本. zlf@ubuntu:~/$ which ldd /usr/bin/ldd zlf@ubuntu:~/$ file /usr/bin/ldd / ...

  9. iOS燃烧动画、3D视图框架、天气动画、立体相册、微信朋友圈小视频等源码

    iOS精选源码 iOS天气动画,包括太阳,云,雨,雷暴,雪动画. 较为美观的多级展开列表 3D立体相册,可以旋转的立方体 一个仪表盘Demo YGDashboardView 一个基于UIScrollV ...

  10. Redis实现分布式读写锁(Java基于Lua实现)

    https://blog.csdn.net/grandachn/article/details/89032815 https://blog.csdn.net/xingsilong/article/de ...