Skip to content

Instantly share code, notes, and snippets.

@diffstorm
Created August 13, 2023 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diffstorm/a8b4ac1d8f917085a96ad348468deed1 to your computer and use it in GitHub Desktop.
Save diffstorm/a8b4ac1d8f917085a96ad348468deed1 to your computer and use it in GitHub Desktop.
Simple file operations class to write into and read from files in a few formats (CSV, JSON, XML, Plain text) with error handling
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <nlohmann/json.hpp>
#include <tinyxml2.h>
enum class FileFormat {
PlainText,
CSV,
JSON,
XML
};
class FileReadWriteLibrary {
public:
bool write(const std::string& filename, FileFormat format, const std::string& data) {
std::ofstream file(filename);
if (!file.is_open()) {
std::cerr << "Failed to open file for writing: " << filename << std::endl;
return false;
}
switch (format) {
case FileFormat::PlainText:
case FileFormat::CSV:
case FileFormat::JSON:
file << data;
break;
case FileFormat::XML:
file << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
file << data;
break;
}
if (!file) {
std::cerr << "Error occurred while writing to file: " << filename << std::endl;
return false;
}
file.close(); // Close the file after writing
return true;
}
bool read(const std::string& filename, FileFormat format, std::string& data) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Failed to open file for reading: " << filename << std::endl;
return false;
}
std::string line;
while (std::getline(file, line)) {
data += line + "\n";
}
if (!file) {
std::cerr << "Error occurred while reading from file: " << filename << std::endl;
return false;
}
file.close(); // Close the file after reading
return true;
}
};
int main() {
FileReadWriteLibrary fileLib;
// Example write operations
std::string dataToWrite = "Hello, World!";
fileLib.write("text_file.txt", FileFormat::PlainText, dataToWrite);
std::string csvData = "Name,Age,Country\nAlice,25,USA\nBob,30,Canada\nCharlie,22,UK\n";
fileLib.write("data.csv", FileFormat::CSV, csvData);
nlohmann::json jsonData = {{"name", "Alice"}, {"age", 25}, {"country", "USA"}};
fileLib.write("data.json", FileFormat::JSON, jsonData.dump(2));
tinyxml2::XMLDocument xmlData;
xmlData.Parse("<root>Hello, XML!</root>");
tinyxml2::XMLPrinter xmlPrinter;
xmlData.Print(&xmlPrinter);
fileLib.write("data.xml", FileFormat::XML, xmlPrinter.CStr());
// Example read operations
std::string readData;
fileLib.read("text_file.txt", FileFormat::PlainText, readData);
std::cout << "Read Plain Text:\n" << readData << std::endl;
readData.clear();
fileLib.read("data.csv", FileFormat::CSV, readData);
std::cout << "Read CSV:\n" << readData << std::endl;
readData.clear();
fileLib.read("data.json", FileFormat::JSON, readData);
std::cout << "Read JSON:\n" << readData << std::endl;
readData.clear();
fileLib.read("data.xml", FileFormat::XML, readData);
std::cout << "Read XML:\n" << readData << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment