CMS 3D CMS Logo

binary_ofstream.cc
Go to the documentation of this file.
1 #include "binary_ofstream.h"
2 
3 #include <cstdio>
4 #include <iostream>
5 #include <cstdint>
6 
8 
9 binary_ofstream::binary_ofstream(const char* name) : file_(nullptr) { init(name); }
10 
11 binary_ofstream::binary_ofstream(const std::string& name) : file_(nullptr) { init(name.c_str()); }
12 
13 void binary_ofstream::init(const char* name) {
14  file_ = fopen(name, "wb");
15  if (file_ == nullptr) {
16  std::cout << "file " << name << " cannot be opened for writing" << std::endl;
17  throw binary_ofstream_error();
18  }
19 }
20 
23  if (file_ != nullptr)
24  fclose(file_);
25  file_ = nullptr;
26 }
27 
29  fputc(n, file_);
30  return *this;
31 }
33  fputc(n, file_);
34  return *this;
35 }
36 
38  fwrite(&n, sizeof(n), 1, file_);
39  return *this;
40 }
42  fwrite(&n, sizeof(n), 1, file_);
43  return *this;
44 }
46  fwrite(&n, sizeof(n), 1, file_);
47  return *this;
48 }
50  fwrite(&n, sizeof(n), 1, file_);
51  return *this;
52 }
54  fwrite(&n, sizeof(n), 1, file_);
55  return *this;
56 }
58  fwrite(&n, sizeof(n), 1, file_);
59  return *this;
60 }
62  fwrite(&n, sizeof(n), 1, file_);
63  return *this;
64 }
66  fwrite(&n, sizeof(n), 1, file_);
67  return *this;
68 }
69 
70 binary_ofstream& binary_ofstream::operator<<(bool n) { return operator<<(static_cast<char>(n)); }
71 
73  (*this)
74  << (uint32_t)
75  s.size(); // Use uint32 for backward compatibilty of binary files that were generated on 32-bit machines.
76  fwrite(s.data(), 1, s.size(), file_);
77  return *this;
78 }
void init(const char *name)
binary_ofstream(const char *name)
binary_ofstream & operator<<(char n)