Files

45 lines
929 B
C++

#include "fileCommon.h"
#include "json.hpp"
bool OpenFile(fs::path path) {
path = fs::absolute(path);
if (!fs::exists(path)) {
printf("File Read error! Could not find file at: \n");
printf("%s\n", path.c_str());
return false;
}
stream.open(absolute(path));
if (!stream) {
printf("Error opening stream \n");
return false;
}
return true;
}
nlohmann::json GetData() {
if (!stream) {
printf("Open the file first! \n");
return nlohmann::json();
}
return nlohmann::json::parse(stream);
}
bool SaveJsonFile(fs::path path, nlohmann::json j) {
if (stream) {
printf("Stream open for reading! Please close stream before saving \n");
return false;
}
stream.open(absolute(path), std::fstream::in | std::fstream::out | std::fstream::app);
stream << std::setw(4) << j << std::endl;
stream.close();
}