Below is how my main.cpp currently looks:
Code: Select all
#include "include/prefixes.h"
#include "include/round_to.h"
#include "include/replace.h"
#include "include/timestamp.h"
#include "include/flags.h"
#include <iostream>
#include <filesystem>
#include <fstream>
#include <array>
#include <chrono>
#include <gflags/gflags.h>
int main(int argc, char *argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
std::cout << "argc: " << argc << '\n';
const std::array<uint64_t, 6> preset_byte_values = {1000, 1024, 1000000, 1048576, 1000000000, 1073741824};
auto UTC_prog_start_time = std::chrono::system_clock::now();
std::string test_file_name_str = "";
try {
test_file_name_str = replace::replace_char(' ', '_', timestamp::to_UTC(UTC_prog_start_time));
} catch (const std::invalid_argument& ex) {
std::cout << ex.what() << '\n';
}
std::string file_path_str = "test_files/File_Gen_Test_File_" + test_file_name_str + ".txt";
if (argc > 1) {
if (FLAGS_use_presets) {
std::cout << "Using preset values." << '\n';
//Using preset values.
try {
std::fstream test_fs{file_path_str, std::ios::out}; //Creating a file stream object to output to.
test_fs.put('a'); //Puts a single character into the test file.
} catch (const std::exception& e) {
std::cout << e.what() << '\n';
}
std::filesystem::path path = file_path_str;
try {
std::filesystem::resize_file(path, preset_byte_values[FLAGS_byte_select]); //Resize file to specified size.
} catch (const std::filesystem::filesystem_error& e) {
std::cout << e.what() << '\n';
}
try {
std::cout << "File size (binary prefix): " << prefix::to_binary_prefix(std::filesystem::file_size(path)) << '\n';
} catch (const std::filesystem::filesystem_error& e) {
std::cout << e.what() << '\n';
} catch (const std::invalid_argument& e) {
std::cout << e.what() << '\n';
}
try {
std::cout << "File size (decimal prefix): " << prefix::to_decimal_prefix(std::filesystem::file_size(path)) << '\n';
} catch (const std::filesystem::filesystem_error& e) {
std::cout << e.what() << '\n';
} catch (const std::invalid_argument& e) {
std::cout << e.what() << '\n';
}
} else {
std::cout << "Not using preset values." << '\n';
//Not using preset values.
try {
std::fstream test_fs{file_path_str, std::ios::out}; //Creating a file stream object to output to.
test_fs.put('a'); //Puts a single character into the test file.
} catch (const std::exception& e) {
std::cout << e.what() << '\n';
}
std::filesystem::path path = file_path_str;
try {
std::filesystem::resize_file(path, FLAGS_fsize); //Resize file to specified size.
} catch (const std::filesystem::filesystem_error& e) {
std::cout << e.what() << '\n';
}
try {
std::cout << "File size (binary prefix): " << prefix::to_binary_prefix(std::filesystem::file_size(path)) << '\n';
} catch (const std::filesystem::filesystem_error& e) {
std::cout << e.what() << '\n';
} catch (const std::invalid_argument& e) {
std::cout << e.what() << '\n';
}
try {
std::cout << "File size (decimal prefix): " << prefix::to_decimal_prefix(std::filesystem::file_size(path)) << '\n';
} catch (const std::filesystem::filesystem_error& e) {
std::cout << e.what() << '\n';
} catch (const std::invalid_argument& e) {
std::cout << e.what() << '\n';
}
}
} else {
std::cout << "No arguments were given." << '\n';
std::exit(EXIT_FAILURE);
}
return 0;
}
https://github.com/AlexLandherr/file_gen
Here's how it looks when I try to run it without any flags/arguments:
Code: Select all
alexl@PD70PNP:/mnt/Storage_SSD/C++_Projects/file_gen$ ./file_gen
argc: 1
Not using preset values.
File size (binary prefix): 0 B
File size (decimal prefix): 0 B
What am I doing wrong here? Should I use this method instead?:
https://stackoverflow.com/questions/542 ... mmand-line