added io functionality

This commit is contained in:
2026-01-11 22:19:00 +01:00
parent b171f11982
commit 5fa62d64ee
2 changed files with 49 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
#include "io/io.hpp"
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string>
std::string read_file(const std::string &FILE_PATH) {
std::ifstream file(FILE_PATH);
if (!file) {
throw std::runtime_error("Error while opening file at " + FILE_PATH);
}
std::stringstream buffer;
buffer << file.rdbuf();
if (file.bad()) {
throw std::runtime_error("Error while reading file at " + FILE_PATH);
}
return buffer.str();
}
void write_file(const std::string &FILE_PATH, const std::string &CONTENT) {
std::ofstream out(FILE_PATH);
if (!out) {
throw std::runtime_error("Error while opening file for writing at " +
FILE_PATH);
}
out << CONTENT;
if (!out) {
throw std::runtime_error("Error while writing to the " + FILE_PATH);
}
out.close();
}

View File

@@ -0,0 +1,13 @@
#include <string>
// @brief Read file from given path
// @param FILE PATH Path to the file
// @return File content
// @throws std::runtime_error if any errors
std::string read_file(const std::string &FILE_PATH);
/// @brief Writes the given content into a file.
/// @param FILE_PATH Path to the file to write.
/// @param CONTENT Content to write into the file.
/// @throws std::runtime_error if the file cannot be opened or written.
void write_file(const std::string &FILE_PATH, const std::string &CONTENT);