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 字节

相关推荐
CodeCraft Studio5 小时前
Excel处理控件Aspose.Cells教程:使用 C# 从 Excel 进行邮件合并
开发语言·c#·excel
小超爱编程6 小时前
纯前端做图片压缩
开发语言·前端·javascript
我不是加奈6 小时前
QMC5883L的驱动
c语言·驱动开发·单片机·嵌入式硬件
青小莫7 小时前
数据结构-C语言-链表OJ
c语言·数据结构·链表
曼巴UE58 小时前
UE5 音效系统
c++·游戏·ue5·虚幻·音效
KIDAKN8 小时前
java--怎么定义枚举类
java·开发语言
海天胜景8 小时前
C# 中常用的 字符串截取方法
开发语言·c#
无影无踪的青蛙8 小时前
[C++] list双向链表使用方法
c++·链表·list
tkevinjd9 小时前
C++中线程库的基本操作
开发语言·c++
CodeWithMe9 小时前
【C/C++】不同防止头文件重复包含的措施
c语言·开发语言·c++