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

相关推荐
Want5959 分钟前
C/C++贪吃蛇小游戏
c语言·开发语言·c++
豆浆whisky1 小时前
Go并发模式选择指南:找到最适合你项目的并发方案|Go语言进阶(19)
开发语言·后端·golang
草莓熊Lotso1 小时前
《算法闯关指南:动态规划算法--斐波拉契数列模型》--01.第N个泰波拉契数,02.三步问题
开发语言·c++·经验分享·笔记·其他·算法·动态规划
胖咕噜的稞达鸭2 小时前
自定义shell命令行解释器自制
java·开发语言
草莓熊Lotso2 小时前
Git 分支管理:从基础操作到协作流程(本地篇)
大数据·服务器·开发语言·c++·人工智能·git·sql
报错小能手2 小时前
C++异常处理 终极及总结
开发语言·c++
雨落在了我的手上2 小时前
C语言入门(二十二):字符函数和字符串函数(2)
c语言
Algo-hx2 小时前
C++编程基础(九):预处理指令
c++
qq_401700416 小时前
嵌入式用Unix时间的优势及其C语言转换
服务器·c语言·unix
tobebetter95278 小时前
How to manage python versions on windows
开发语言·windows·python