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

相关推荐
梦雨生生13 分钟前
java开发工具(学习第一天)
java·开发语言·学习
啊啊啊啊啊!!!!2 小时前
【c++】stack和queue的接口使用以及底层实现
开发语言·c++
松仔log3 小时前
Java中级——组合和继承
android·java·开发语言
上海安当技术3 小时前
C/S 桌面软件怎么接 UKey 强认证?C# SDK 与 C 动态库集成 WinForms/Qt/工控实战
c语言·qt·c#
(Charon)4 小时前
【C++】锁与原子操作(四):自旋锁的完整实现与性能优化
c语言·开发语言·c++
雨田言炎5 小时前
五、Qt控件使用说明大全
开发语言·qt
ShineWinsu5 小时前
对于Linux:五种IO模型以及非阻塞IO的详细解析
linux·c++·面试·io·阻塞·非阻塞·fcntl
bu_shuo5 小时前
MATLAB中的meshgrid和ndgrid
开发语言·matlab
小羊先生car6 小时前
RTOS-F429-HAL-Freertos内存管理(2026/8/5)
c语言·单片机·rtos
我命由我123456 小时前
Jetpack Compose - MaterialExpressiveTheme 与 MaterialTheme、ColorScheme
android·java·开发语言·java-ee·kotlin·android jetpack·android runtime