Advertisement
Guest User

Lab 2

a guest
Oct 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. point.h:
  2.  
  3. #pragma once
  4.  
  5. struct Point {
  6. double x, y, z;
  7. Point() : x(0.0), y(0.0), z(0.0) {}
  8. Point(double x, double y, double z) : x(x), y(y), z(z) {}
  9. };
  10.  
  11.  
  12.  
  13. FileError.h:
  14.  
  15. #pragma once
  16.  
  17.  
  18. enum FileError {
  19. SUCCESS,
  20. ACCESS_DENIED,
  21. OUT_OF_BOUNDS,
  22. FILE_INVALID
  23. };
  24.  
  25.  
  26. BinaryFile.h:
  27.  
  28. #pragma once
  29. #include <string>
  30. #include <fstream>
  31. #include <vector>
  32.  
  33. #include "FileError.h"
  34. #include "Point.h"
  35.  
  36. using namespace std;
  37.  
  38. class BinaryFile
  39. {
  40. private:
  41. fstream file;
  42. string path;
  43. int fileSize;
  44. fstream::openmode openMode;
  45.  
  46. public:
  47. BinaryFile(const string path, fstream::openmode openMode);
  48.  
  49. FileError Write(const vector<Point>& points);
  50. FileError Read(vector<Point>& points);
  51. FileError Read(Point& point, const int index);
  52. };
  53.  
  54.  
  55. main:
  56.  
  57. #include <vector>
  58.  
  59. #include "BinaryFile.h"
  60. #include "Point.h"
  61.  
  62. using namespace std;
  63.  
  64. int main()
  65. {
  66. BinaryFile binaryFile("test.bin", fstream::out | fstream::in | fstream::trunc);
  67.  
  68. vector<Point> points;
  69. for (int i = 0; i < 5; i++)
  70. {
  71. Point point(i, i + 1.0, i + 2.0);
  72. points.push_back(point);
  73. }
  74.  
  75. binaryFile.Write(points);
  76. }
  77.  
  78.  
  79. BinaryFile.cpp:
  80.  
  81. #include "BinaryFile.h"
  82.  
  83. BinaryFile::BinaryFile(const string path, fstream::openmode openMode)
  84. {
  85. this->path = path;
  86. //...
  87. this->openMode = openMode | fstream::binary;
  88. file.open(path, this->openMode);
  89. }
  90.  
  91. FileError BinaryFile::Write(const vector<Point>& points)
  92. {
  93. if (!this->file.is_open())
  94. return FileError::FILE_INVALID;
  95.  
  96. if (!(openMode & fstream::out))
  97. return FileError::ACCESS_DENIED;
  98.  
  99.  
  100. int bytes = points.size() * sizeof(Point);
  101. file.write((const char*)points.data(), bytes);
  102.  
  103. return FileError::SUCCESS;
  104. }
  105.  
  106. FileError BinaryFile::Read(vector<Point>& points)
  107. {
  108. if (!this->file.is_open())
  109. return FileError::FILE_INVALID;
  110.  
  111. if (!(openMode & fstream::in))
  112. return FileError::ACCESS_DENIED;
  113.  
  114. file.seekg(0, file.end);
  115. int bytes = file.tellg();
  116. file.seekg(0, file.beg);
  117.  
  118. if (bytes <= 0)
  119. return FileError::OUT_OF_BOUNDS;
  120.  
  121. int pointsCount = bytes / sizeof(Point);
  122. points = vector<Point>(pointsCount);
  123.  
  124. file.read((char*)points.data(), bytes);
  125.  
  126. return FileError::SUCCESS;
  127. }
  128.  
  129. FileError BinaryFile::Read(Point& point, const int index)
  130. {
  131. return FileError();
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement