Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (62 sloc) 2.17 KB
//
// Program
// C++17 Filesystem: Shows current and temporary directories and evaluates disk sizes.
//
// Compiler
// Make sure that gcc support c++17 with file system module.
// - Use gcc-8 (g++ (Ubuntu 8.2.0-1ubuntu2~18.04) 8.2.0): sudo apt install gcc-8
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -o 01-fs_test 01-fs_test.cxx -lstdc++fs
//
// Execution
// ./01-fs_test
//
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
constexpr uintmax_t bytes_kilo = 1LL << 10;
constexpr uintmax_t bytes_mega = 1LL << 20;
constexpr uintmax_t bytes_giga = 1LL << 30;
constexpr uintmax_t bytes_tera = 1LL << 40;
// Convert bytes into std::string using storage units
std::string bytestostring(uintmax_t bytes) {
long double bytes_f = bytes * 1.0f;
if (bytes > bytes_tera)
{
return std::to_string( bytes_f / bytes_tera) + std::string(" TB");
}
else if (bytes > bytes_giga)
{
return std::to_string(bytes_f / bytes_giga) + std::string(" GB");
}
else if (bytes > bytes_mega)
{
return std::to_string(bytes_f / bytes_mega) + std::string(" MB");
}
else if (bytes > bytes_kilo)
{
return std::to_string(bytes_f / bytes_kilo) + std::string(" KB");
}
return std::to_string(bytes_f) + std::string(" Bytes");
}
//
// Entry function
//
int main(int argc, char *argv[]) {
fs::path curr_path = fs::current_path();
fs::path tmp_path = fs::temp_directory_path();
std::cout << "Hello world -- std::filesystem" << std::endl;
std::cout << " - Current working directory: " << curr_path.string() << std::endl;
std::cout << " - Temporary directory: " << tmp_path.string() << std::endl;
fs::space_info info = fs::space(curr_path);
std::cout << std::endl;
std::cout << " - Directory info: " << curr_path.string() << std::endl;
std::cout << " - Total size: " << bytestostring(info.capacity) << std::endl;
std::cout << " - Free space size: " << bytestostring(info.free) << std::endl;
std::cout << " - Available size: " << bytestostring(info.available) << std::endl;
// Suppress compiler warning(s)
(void)argc;
(void)argv;
return 0;
}
You can’t perform that action at this time.