Skip to content

Instantly share code, notes, and snippets.

@AtomicVar
Created November 26, 2023 10:00
Show Gist options
  • Save AtomicVar/a41029370ad2e234688b0c4a92885f5b to your computer and use it in GitHub Desktop.
Save AtomicVar/a41029370ad2e234688b0c4a92885f5b to your computer and use it in GitHub Desktop.
将字节数打印为人类易读的格式(KB/MB/GB)
struct HumanReadable
{
std::uintmax_t size{};
private:
friend std::ostream& operator<<(std::ostream& os, HumanReadable hr)
{
int o{};
double mantissa = hr.size;
for (; mantissa >= 1024.; mantissa /= 1024., ++o);
os << std::ceil(mantissa * 10.) / 10. << "BKMGTPE"[o];
return o ? os << "B (" << hr.size << ')' : os;
}
};
@AtomicVar
Copy link
Author

AtomicVar commented Nov 26, 2023

用法示例:

#include <cmath>
#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;
 
struct HumanReadable
{
    std::uintmax_t size{};
 
private:
    friend std::ostream& operator<<(std::ostream& os, HumanReadable hr)
    {
        int o{};
        double mantissa = hr.size;
        for (; mantissa >= 1024.; mantissa /= 1024., ++o);
        os << std::ceil(mantissa * 10.) / 10. << "BKMGTPE"[o];
        return o ? os << "B (" << hr.size << ')' : os;
    }
};
 
int main(int, char const* argv[])
{
    fs::path example = "example.bin";
    fs::path p = fs::current_path() / example;
    std::ofstream(p).put('a'); // create file of size 1
    std::cout << example << " size = " << fs::file_size(p) << '\n';
    fs::remove(p);
 
    p = argv[0];
    std::cout << p << " size = " << HumanReadable{fs::file_size(p)} << '\n';
 
    try
    {
        std::cout << "Attempt to get size of a directory:\n";
        [[maybe_unused]] auto x_x = fs::file_size("/dev");
    }
    catch (fs::filesystem_error& e)
    {
        std::cout << e.what() << '\n';
    }
 
    for (std::error_code ec; fs::path bin : {"cat", "mouse"})
    {
        bin = "/bin"/bin;
        if (const std::uintmax_t size = fs::file_size(bin, ec); ec)
            std::cout << bin << " : " << ec.message() << '\n';
        else
            std::cout << bin << " size = " << HumanReadable{size} << '\n';
    }
}

可能的输出:

"example.bin" size = 1
"./a.out" size = 22KB (22512)
Attempt to get size of a directory:
filesystem error: cannot get file size: Is a directory [/dev]
"/bin/cat" size = 50.9KB (52080)
"/bin/mouse" : No such file or directory

参考:std::filesystem::file_size - cppreference.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment