Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BinaryFile.h"
- BinaryFile::BinaryFile(std::string path, std::string mode) : path(path)
- {
- openMode = std::fstream::ate | std::fstream::binary;
- if (mode.find("w") != std::string::npos) {
- openMode = openMode | std::fstream::out;
- }
- if (mode.find("r") != std::string::npos) {
- openMode = openMode | std::fstream::in;
- }
- if (mode.find("a") != std::string::npos) {
- openMode = openMode | std::fstream::app;
- }
- if (mode.find("t") != std::string::npos) {
- openMode = openMode | std::fstream::trunc;
- }
- file.open(path, openMode);
- size = file.tellg();
- file.seekg(0, file.beg);
- }
- BinaryFile::~BinaryFile()
- {
- file.close();
- }
- FileError BinaryFile::write(std::vector<Point>& points)
- {
- if (!file.is_open()) {
- return FileError::FILE_INVALID;
- }
- if (!(openMode & std::fstream::in)) {
- return FileError::ACCESS_DENIED;
- }
- for (auto p : points)
- {
- file.write((char*)&p, sizeof(Point));
- }
- return FileError::SUCCESS;
- }
- FileError BinaryFile::read(std::vector<Point>& points)
- {
- if (!file.is_open()) {
- return FileError::FILE_INVALID;
- }
- if (!(openMode & std::fstream::out)) {
- return FileError::ACCESS_DENIED;
- }
- points.clear();
- file.seekg(0);
- Point Hindus(5, 6, 7);
- while (file.tellg() < size) {
- file.read((char*)&Hindus, sizeof(Point));
- points.push_back(Point(Hindus.x, Hindus.y, Hindus.z));
- }
- return FileError::SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement