-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryWrite.h
More file actions
64 lines (47 loc) · 1.27 KB
/
BinaryWrite.h
File metadata and controls
64 lines (47 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* BinaryWrite.h
*
* Copyright 2018 OFTNAI. All rights reserved.
*
*/
#ifndef BINARYWRITE_H
#define BINARYWRITE_H
// Forward declarations
// Includes
#include <fstream>
#include <string>
#include <iostream>
#include <cerrno>
#include <cstdlib>
#include <cstring>
using std::string;
using std::fstream;
using std::cerr;
using std::endl;
class BinaryWrite : public fstream {
private:
const char * filename;
public:
// Constructors
BinaryWrite();
BinaryWrite(const char * filename);
BinaryWrite(const string & filename);
void openFile(const char * file);
void openFile(const string & file);
// Overloaded output/input ops. resp.
template <class T> BinaryWrite & operator<<(T val);
};
// We include code here because of templates definitions having to be visible at compile time
// in each translation unit
template <class T>
BinaryWrite & BinaryWrite::operator<<(T val) {
try {
write(reinterpret_cast<char*>(&val), sizeof(T));
} catch (fstream::failure e) {
cerr << "Unable to write to: error = " << strerror(errno) << ", file = " << filename << endl;
cerr.flush();
exit(EXIT_FAILURE);
}
return *this;
}
#endif // BINARYWRITE_H