控制台输出彩色基础信息

功能作用

可输出启动参数、环境变量、文件属性。
可进行彩色输出。
用于显示程序运行情况。

涉及知识点

  • #defind 的使用,字符串和数字的拼接。
  • 彩色控制台输出代码及简化。
  • 文件信息的读取
  • 获取当前时间
  • 时间转字符串

完整代码

Windows 下使用 VS Code 在 minggw 终端下进行的代码编写,未考虑兼容性。

HelloWorld.cpp :

#include <iostream>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef WIN32
#include <unistd.h>
#else
#define stat _stat
#endif

using namespace std;

namespace FILE_INFO_UTILS{
   
    class FileInfoUtils{
   
    public:
        FileInfoUtils(char*filepath) : m_filePath(filepath) {
   
            _stat64(filepath, &m_stat);
        }
        const struct _stat64 stat() const {
    return m_stat; }
        const std::string filePath() const {
    return m_filePath; }
        // std::string fileName() { return m_filePath; }
        // std::string fileSuffix() { return m_filePath; }
        // std::string dirName() { return m_filePath; }
        // std::string mode() { return std::string("-rwxrwxrwx"); }
        const __int64 fileSize() const {
    return m_stat.st_size; }  // win(1024)/mac(1000)
        // const std::string humanSize() const { return std::string("xxx bytes/KB/MB/GB/TB/PB/..."); }
        const std::string modifyTime() {
    return timeToStr(m_stat.st_mtime); }
        const std::string createTime() {
    return timeToStr(m_stat.st_ctime);; }
        const std::string activeTime() {
    return timeToStr(m_stat.st_atime);; }
    private:
        std::string timeToStr(const __time64_t time) {
   
            struct tm tmStruct = {
   };
            char timeChar[26] = {
   };
            localtime_s(&tmStruct, &time);
            strftime(timeChar, sizeof(timeChar), "%Y-%m-%d %H:%M:%S", &tmStr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值