C++ 获取文件创建时间、修改时间、大小等属性

简介

获取文件创建时间、修改时间、大小等属性


代码

cpp 复制代码
#include <iostream>
#include <string.h>
#include <time.h>

void main()
{
    std::string filename = "E:\\LiHai123.txt";
    struct _stat stat_buffer;
    int result = _stat(filename.c_str(), &stat_buffer);

    struct tm tmStruct;
    char timeChar[26];

    localtime_s(&tmStruct, &stat_buffer.st_ctime);
    strftime(timeChar, sizeof(timeChar), "%Y-%m-%d %H:%M:%S", &tmStruct);
    std::cout << "创建时间: " << timeChar << std::endl;

    localtime_s(&tmStruct, &stat_buffer.st_mtime);
    strftime(timeChar, sizeof(timeChar), "%Y-%m-%d %H:%M:%S", &tmStruct);
    std::cout << "修改时间: " << timeChar << std::endl;

    localtime_s(&tmStruct, &stat_buffer.st_atime);
    strftime(timeChar, sizeof(timeChar), "%Y-%m-%d %H:%M:%S", &tmStruct);
    std::cout << "访问时间: " << timeChar << std::endl;
   
    std::cout << "文件大小: " << stat_buffer.st_size << " 字节" <<std::endl;

    /*
    std::cout << "设备号: " << stat_buffer.st_dev << std::endl;
    std::cout << "索引号: " << stat_buffer.st_ino << std::endl;
    std::cout << "文件类型和访问权限标志: " << stat_buffer.st_mode << std::endl;
    std::cout << "硬链接数量: " << stat_buffer.st_nlink << std::endl;
    std::cout << "所有者的用户标识符: " << stat_buffer.st_uid << std::endl;
    std::cout << "所有者的组标识符: " << stat_buffer.st_gid << std::endl;
    */

    std::cin.get();
}

输出:

创建时间: 2023-10-10 14:41:48

修改时间: 2023-10-10 15:12:39

访问时间: 2023-10-10 16:59:26

文件大小: 1892 字节

相关推荐
Chenyu_3104 小时前
12.找到字符串中所有字母异位词
c语言·数据结构·算法·哈希算法
门前云梦6 小时前
《C语言·源初法典》---C语言基础(上)
c语言·开发语言·学习
achene_ql6 小时前
select、poll、epoll 与 Reactor 模式
linux·服务器·网络·c++
sjtu_cjs7 小时前
Tensorrt python api 10.11.0笔记
开发语言·笔记·python
哆啦A梦的口袋呀7 小时前
深入理解系统:UML类图
开发语言·python·uml
虎冯河7 小时前
怎么让Comfyui导出的图像不包含工作流信息,
开发语言·python
coding随想7 小时前
JavaScript中的原始值包装类型:让基本类型也能“变身”对象
开发语言·javascript·ecmascript
SY师弟7 小时前
51单片机——计分器
c语言·c++·单片机·嵌入式硬件·51单片机·嵌入式
2301_794333918 小时前
Maven 概述、安装、配置、仓库、私服详解
java·开发语言·jvm·开源·maven