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. ubuntu18安装pytorch1.3

    环境: ubuntu18 anaconda 创建一个新的环境 conda create -n env_name python=version 激活并进入环境中 conda activate env_n ...

  2. 10.PoolArena

    PoolArena PoolArena成员介绍 PoolChunkList PoolChunkList实例化 PoolChunkList添加PoolChunk PoolChunkList移动PoolC ...

  3. 安装 texlive

    多系统使用texlive 中文latex 用xelatex 编译 只需要加入宏包 \usepackage[UTF8]{ctex} Rmarkdown 配置模版 $ cat _output.yaml b ...

  4. debian8.8安装sougou输入法

    传送门:http://www.cnblogs.com/ligongzi/p/6137601.html 亲测可用

  5. 两种大小写比较|elif|

    name = ['alle','mike','tom','jerry','alice','hebe'] for i in name: if i == 'tom': print 'get!' #get! ...

  6. Opencv笔记(四)——绘图函数

    常用的绘图函数有: cv2.line()       cv2.circle()        cv2.rectangle()      cv2.ellipse()       cv2.putText( ...

  7. 方格取数(多线程dp,深搜)

    https://www.luogu.org/problem/P1004 题目描述 设有N×N的方格图(N≤9),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字0.如下图所示(见样例): 某 ...

  8. docker容器中安装中文字体

    在项目中用到pdf导出功能,需要安装中文字体,项目使用docker部署,为了方便决定在将字体安装在镜像中. 1.在dockerfile文件中添加字体copy语句(本次用是的宋体,字体源文件放在dock ...

  9. [LC] 348. Design Tic-Tac-Toe

    Design a Tic-tac-toe game that is played between two players on a nx n grid. You may assume the foll ...

  10. 吴裕雄--天生自然python学习笔记:python 建立 Firebase 数据库连接

    Python 程序通过 python-firebase 包可以存取 Firebase 数据库. 使用 python-firebase 包 首先必须安装 python-firebase 包,安装方法如下 ...