首页 > 编程语言 >C++数据格式化2 - 将文件大小转换为人类易读的格式

C++数据格式化2 - 将文件大小转换为人类易读的格式

时间:2024-06-18 22:59:21浏览次数:10  
标签:易读 文件大小 string double fmt C++ std include size

1. 关键词

C++ 数据格式化 字符串处理 std::string 文件大小 跨平台

2. strfmt.h

#pragma once

#include <string>
#include <cstdint>
#include <sstream>
#include <iomanip>

namespace cutl
{
    /**
     * @brief Format a file size to a human-readable string with a given precision.
     *
     * @param size the size to be formatted.
     * @param simplify whether to use a simplify unit.
     * @param precision the precision of the formatted string, default is 1.
     * @return std::string the formatted string.
     */
    std::string fmt_filesize(uint64_t size, bool simplify = true, int precision = 1);
} // namespace cutl

3. strfmt.cpp

#include <sstream>
#include <iomanip>
#include <bitset>
#include "strfmt.h"

namespace cutl
{
    std::string fmt_filesize(uint64_t size, bool simplify, int precision)
    {
        static const double KBSize = 1024;
        static const double MBSize = 1024 * 1024;
        static const double GBSize = 1024 * 1024 * 1024;
        const std::string gb = simplify ? "G" : "GB";
        const std::string mb = simplify ? "M" : "MB";
        const std::string kb = simplify ? "K" : "KB";
        const std::string byte = simplify ? "B" : "Byte";

        if (size > GBSize)
        {
            double hSize = (double)size / GBSize;
            return fmt_double(hSize, precision) + gb;
        }
        else if (size > MBSize)
        {
            double hSize = (double)size / MBSize;
            return fmt_double(hSize, precision) + mb;
        }
        else if (size > KBSize)
        {
            double hSize = (double)size / KBSize;
            return fmt_double(hSize, precision) + kb;
        }
        else
        {
            return fmt_double(size, precision) + byte;
        }

        return "";
    }
} // namespace cutl

4. 测试代码

#include "common.hpp"
#include "strfmt.h"

void TestFormatFileSize()
{
    PrintSubTitle("TestFormatFileSize");

    std::cout << "fmt_filesize 1: " << cutl::fmt_filesize(378711367) << std::endl;
    std::cout << "fmt_filesize 2: " << cutl::fmt_filesize(378711367, true, 2) << std::endl;
    std::cout << "fmt_filesize 2: " << cutl::fmt_filesize(378711367, false, 2) << std::endl;
}

5. 运行结果

-----------------------------------------TestFormatFileSize-----------------------------------------
fmt_filesize 1: 361.2M
fmt_filesize 2: 361.17M
fmt_filesize 2: 361.17MB

6. 源码地址

更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。

标签:易读,文件大小,string,double,fmt,C++,std,include,size
From: https://www.cnblogs.com/luoweifu/p/18255359

相关文章

  • C++数据格式化1 - uint转换成字符串 & double转换成字符串
    1.关键词2.strfmt.h3.strfmt.cpp4.测试代码5.运行结果6.源码地址1.关键词C++数据格式化字符串处理std::stringintdouble跨平台2.strfmt.h#pragmaonce#include<string>#include<cstdint>#include<sstream>#include<iomanip>namespacecutl{......
  • 跟我从零开始学C++(C++代码基础)2
    引言在上一章我们下载了学习C++的工具,VisualStudio编译器,也学习了一些简单基础的语法,知道了一些C++的相关的背景知识,还有C++的语法基础,我们这节课就来接着学习有关C++的内容。本章的内容有运算符和表达式,以及控制结构。编写我们的第一个代码Helloworld!不论是是学习什么......
  • C++PrimerPlus:第十三章类和继承:访问控制:protected
    第十三章类和继承:访问控制:protected提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加例如:访问控制:protected提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录第十三章类和继承:访问控制:protected前言一、访问控制:protected总结......
  • 理解 C++ 中的对象类型与绑定机制:静态绑定 vs 动态绑定
    静态绑定和动态绑定概念解释对象的静态类型:对象在声明时采用的类型,在编译期确定,无法更改。对象的动态类型:对象在运行期实际表现的类型,在运行期决定,对象的动态类型可以更改(通过多态和指针/引用的方式)。静态绑定:绑定的是对象的静态类型,某特性(比如函数调用)依赖于对象的静......
  • 《C++ Primer》导学系列:第 5 章 - 语句
    5.1简单语句5.1.1基本概念在C++中,简单语句是构成程序的基本单元。每个简单语句以分号(;)结束,可以是表达式、声明或空语句。5.1.2表达式语句表达式语句是由表达式构成的语句,通常用于计算、赋值和函数调用。示例代码#include<iostream>intmain(){inta=10;......
  • c++期末题库三(读程序写结果)
    1.#include<iostream>usingnamespacestd;classBC{public:BC(intn=100){val=n;cout<<"defaultcon."<<endl;}BC(BC&t){val=t.val;cout<<"Copycon."<<endl;}BC&operator=(BC&t){val=t.val;c......
  • VC++ gen uuid and time
    //ConsoleApplication2.cpp:Thisfilecontainsthe'main'function.Programexecutionbeginsandendsthere.//#define_CRT_SECURE_NO_WARNINGS#pragmacomment(lib,"rpcrt4.lib")#include<windows.h>#include<rpcdce.h>#......
  • C/C++ main 函数注意事项
    main 函数是C/C++程序的入口点,当运行程序时,操作系统会调用 main 函数来启动程序。下面为 main 函数的注意事项:函数签名:在C/C++中,main 函数的标准签名通常有两种形式:intmain(){//...}intmain(intargc,char*argv[]){//...}其中,argc 是命......
  • C++ 20新特性之原子引用
    ......
  • 【C/C++】实现高性能日志轮转功能,已实测
    基本实现在C语言中实现日志文件轮转功能,你需要手动编写代码来处理文件的重命名、压缩和删除。下面是一个简单的C语言程序示例,它演示了如何实现基本的日志文件轮转功能。这个程序会检查日志文件的大小,如果超过预设的大小限制,则将当前日志文件重命名,并创建一个新的日志文件。......