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

相关推荐
学逆向的7 小时前
C 堆区内存管理
c语言·开发语言·网络安全
huoyueyi7 小时前
《融天AI-APP从0到1开发实录》
开发语言·lua
xiaoye-duck9 小时前
《C++ 工具库实战》gflags 命令行参数解析库详解
c++·第三方库
OpenAnolis小助手9 小时前
PHP 两个严重漏洞曝光,龙蜥社区 24 小时完成 3 版本修复
开发语言·操作系统·php·龙蜥社区
狂奔的sherry10 小时前
C++ 宏生成访问函数的代价:访问权限、调试和类型检查
c++·宏·编译·预处理器·类型检查
阿尼卡~10 小时前
VS Code 配置 C 语言开发环境(Windows 10/11):从 GCC 安装到运行、断点调试,一篇搞定
c语言·开发语言·windows
划破黑暗的第一缕曙光11 小时前
[C++]:5.STL:string(2)
java·c++·redis
hhhjhl11 小时前
Undici 跨端三方库仓颉适配全流程
java·开发语言·ubuntu
染指111011 小时前
158.Agent-LangGraph-子图(共享状态和不共享状态模式)
开发语言·人工智能·python·ui·agent·agents
wangchunyu11411 小时前
JAVA JVM内存结构与运行时数据区
java·开发语言·jvm