首页 > 编程语言 >C++ 获取文件创建时间、修改时间、大小等属性

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

时间:2023-11-28 10:03:52浏览次数:30  
标签:10 stat timeChar tmStruct 文件创建 C++ buffer 时间 include


简介

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

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


C++ 获取文件创建时间、修改时间、大小等属性_开发语言_02

代码

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


标签:10,stat,timeChar,tmStruct,文件创建,C++,buffer,时间,include
From: https://blog.51cto.com/wangpaifeixingy/8595257

相关文章

  • C++ 获取网卡名称和IP地址
    描述这是获取网卡名称和IP地址的代码示例,参考自。原文描述得比较详细,感谢博主分享。原文代码中没有输出网卡的物理地址,下面的代码进行了补充,并在win10上运行正常。代码//#include<WinSock2.h>#include<Iphlpapi.h>#pragmacomment(lib,"Iphlpapi.lib")//需要添加Iphlpapi.lib......
  • C\C++ 设置Visual Studio编译器使用C++17标准
    文章作者:里海简介:        使用ISOC++17标准可以为开发人员带来许多好处,包括更简洁的代码、更高的运行效率、更好的硬件支持、更好的兼容性和可移植性,以及更好的多线程编程支持等。那么如何设置vs使用c++标准呢?下面是方法。注意需要vs2017及以上版本。方法:打开VisualStud......
  • C++ 33.C++中的字符串类-狄泰软件学院
    C语言字符串的历史C语言不支持真正意义上的字符串C语言用字符数组和一组函数实现字符串操作C语言不支持自定义类型,因此无法创建字符串类型当年C语言主要用于开发UNIX操作系统,处理字符串的情况少,所以在当时的背景下没有让C语言中内置一个字符串类型。后来C语言越用越广泛,没办法只能......
  • C\C++ 专栏目录
    个人总结序号内容笔记01C++获取网卡名称和IP地址笔记链接02C++设置VisualStudio编译器使用C++17标准笔记链接03C++使用Pugixml库,轻松处理XML文件笔记链接04C++使用ShellExecuteEx调exe程序笔记链接05C++使用exception类,抛出自定义异常并捕获笔记链接06C++使用soc......
  • C++标准库类std::packaged_task
    std::packaged_task是C++11引入的标准库类,用于封装可调用对象,如函数等,并将封装对象作为异步任务进行管理,通过与std::future结合使用,完成异步任务结果的获取。#include<iostream>#include<thread>#include<future>std::stringpromise_string(std::stringstr){......
  • 时间工具
    获取Date类型的年,月,日,时,分,秒   获取系统当前时间的年,月,日,时,分,秒 将字符串转换为Date类型 java8是如何处理时间及日期的1、如何在java8中获取当天的日期java8中有个叫LocalDate的类,能用来表示今天的日期。这个类与java.util.Date略有不同,因为它只包含日期,没有时间......
  • 实验4 现代C++标准库与类模板
    实验任务5:1.代码:textcoder.hpp:1#pragmaonce23#include<iostream>4#include<vector>5#include<array>6#include<string>7usingnamespacestd;89classTextCoder10{11private:12stringtext;13......
  • 支持修改键值的优先队列(以C++,Java为例)
    #include<queue>#include<functional>template<typenameT1,typenameT2>classmutable_priority_queue;template<typenameT1,typenameT2>classmutable_priority_queue{private:std::function<bool(conststd::pair<T1,T......
  • C++ vs Python
    WhyC++isfasterthanPythonhttps://www.freecodecamp.org/news/python-vs-c-plus-plus-time-complexity-analysis/SummaryTable编程语言stronglytyped?跨平台语言类型C++YesYes编译型PythonNoYes解释型参考资料stronglytypedprogrammingla......
  • C++标准库类std::shared_future
    std::shared_future是C++11的标准库类,其与std::future的不同是允许多个线程使用,多个线程可以同步共享,同时其又不会阻塞等待异步任务的完成。std::shared_future同样也提供get()方法用于获取异步执行的结果。#include<iostream>#include<thread>#include<future>void......