#include "BinaryFile.h"
BinaryFile::BinaryFile(const string path, fstream::openmode openMode)
{
this->path = path;
//...
this->openMode = openMode | fstream::binary;
file.open(path, this->openMode);
}
FileError BinaryFile::Write(const vector<Point>& points)
{
if (!this->file.is_open())
return FileError::FILE_INVALID;
if (!(openMode & fstream::out))
return FileError::ACCESS_DENIED;
int bytes = points.size() * sizeof(Point);
file.write((const char*)points.data(), bytes);
return FileError::SUCCESS;
}
FileError BinaryFile::Read(vector<Point>& points)
{
if (!this->file.is_open())
return FileError::FILE_INVALID;
if (!(openMode & fstream::in))
return FileError::ACCESS_DENIED;
file.seekg(0, file.end);
int bytes = file.tellg();
file.seekg(0, file.beg);
if (bytes <= 0)
return FileError::OUT_OF_BOUNDS;
int pointsCount = bytes / sizeof(Point);
points = vector<Point>(pointsCount);
file.read((char*)points.data(), bytes);
return FileError::SUCCESS;
}
FileError BinaryFile::Read(Point& point, int index)
{
if (!file.is_open())
return FileError::FILE_INVALID;
if (!(openMode & fstream::in))
return FileError::ACCESS_DENIED;
file.seekg(0, file.end);
int bytes = file.tellg();
file.seekg(0, file.beg);
if (index * sizeof(Point) >= bytes)
return FileError::OUT_OF_BOUNDS;
file.seekg(index * sizeof(Point), file.beg);
file.read((char*)&point, sizeof(Point));
return FileError::SUCCESS;
}
Paste Hosted With By Wklejamy.pl