Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include "BinaryFile.h"
  2.  
  3.  
  4.  
  5.  
  6. BinaryFile::BinaryFile(std::string path, std::string mode) : path(path)
  7. {
  8. openMode = std::fstream::ate | std::fstream::binary;
  9. if (mode.find("w") != std::string::npos) {
  10. openMode = openMode | std::fstream::out;
  11. }
  12. if (mode.find("r") != std::string::npos) {
  13. openMode = openMode | std::fstream::in;
  14. }
  15. if (mode.find("a") != std::string::npos) {
  16. openMode = openMode | std::fstream::app;
  17. }
  18. if (mode.find("t") != std::string::npos) {
  19. openMode = openMode | std::fstream::trunc;
  20. }
  21. file.open(path, openMode);
  22. size = file.tellg();
  23. file.seekg(0, file.beg);
  24. }
  25.  
  26. BinaryFile::~BinaryFile()
  27. {
  28. file.close();
  29. }
  30.  
  31. FileError BinaryFile::write(std::vector<Point>& points)
  32. {
  33. if (!file.is_open()) {
  34. return FileError::FILE_INVALID;
  35. }
  36. if (!(openMode & std::fstream::in)) {
  37. return FileError::ACCESS_DENIED;
  38. }
  39. for (auto p : points)
  40. {
  41. file.write((char*)&p, sizeof(Point));
  42. }
  43. return FileError::SUCCESS;
  44. }
  45.  
  46. FileError BinaryFile::read(std::vector<Point>& points)
  47. {
  48. if (!file.is_open()) {
  49. return FileError::FILE_INVALID;
  50. }
  51. if (!(openMode & std::fstream::out)) {
  52. return FileError::ACCESS_DENIED;
  53. }
  54. points.clear();
  55. file.seekg(0);
  56. Point Hindus(5, 6, 7);
  57. while (file.tellg() < size) {
  58. file.read((char*)&Hindus, sizeof(Point));
  59. points.push_back(Point(Hindus.x, Hindus.y, Hindus.z));
  60. }
  61. return FileError::SUCCESS;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement