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 
7 
8 binary_ofstream::binary_ofstream(const char* name) : file_(nullptr) { init(name); }
9 
10 binary_ofstream::binary_ofstream(const std::string& name) : file_(nullptr) { init(name.c_str()); }
11 
12 void binary_ofstream::init(const char* name) {
13  file_ = fopen(name, "wb");
14  if (file_ == nullptr) {
15  std::cout << "file " << name << " cannot be opened for writing" << std::endl;
16  throw binary_ofstream_error();
17  }
18 }
19 
22  if (file_ != nullptr)
23  fclose(file_);
24  file_ = nullptr;
25 }
26 
28  fputc(n, file_);
29  return *this;
30 }
32  fputc(n, file_);
33  return *this;
34 }
35 
37  fwrite(&n, sizeof(n), 1, file_);
38  return *this;
39 }
41  fwrite(&n, sizeof(n), 1, file_);
42  return *this;
43 }
45  fwrite(&n, sizeof(n), 1, file_);
46  return *this;
47 }
49  fwrite(&n, sizeof(n), 1, file_);
50  return *this;
51 }
53  fwrite(&n, sizeof(n), 1, file_);
54  return *this;
55 }
57  fwrite(&n, sizeof(n), 1, file_);
58  return *this;
59 }
61  fwrite(&n, sizeof(n), 1, file_);
62  return *this;
63 }
65  fwrite(&n, sizeof(n), 1, file_);
66  return *this;
67 }
68 
69 binary_ofstream& binary_ofstream::operator<<(bool n) { return operator<<(static_cast<char>(n)); }
70 
72  (*this)
73  << (uint32_t)
74  s.size(); // Use uint32 for backward compatibilty of binary files that were generated on 32-bit machines.
75  fwrite(s.data(), 1, s.size(), file_);
76  return *this;
77 }
void init(const char *name)
binary_ofstream(const char *name)
binary_ofstream & operator<<(char n)