-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryRead.cpp
More file actions
55 lines (40 loc) · 1.39 KB
/
BinaryRead.cpp
File metadata and controls
55 lines (40 loc) · 1.39 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
/*
* BinaryRead.cpp
*
* Copyright 2018 OFTNAI. All rights reserved.
*
*/
// Forward declarations
// Includes
#include "BinaryRead.h"
#include <cstdlib>
#include <cstring>
using std::cerr;
using std::endl;
BinaryRead::BinaryRead() : fstream() {}
BinaryRead::BinaryRead(const char * filename) : fstream() { openFile(filename); }
BinaryRead::BinaryRead(const string & filename) : fstream() { openFile(filename); }
void BinaryRead::openFile(const string & filename) { openFile(filename.c_str()); }
void BinaryRead::openFile(const char * filename) {
this->filename = filename;
exceptions( std::ios_base::badbit | std::ios_base::failbit);
// badbit = fatal i/o error, e.g. file does note exist
// failbit = non-fatal i/o error, e.g. eof reached
// Open file
try {
open(filename, std::ios_base::in | std::ios_base::binary);
// get length of file:
seekg (0, std::ios::end);
int long long length = tellg();
seekg (0, std::ios::beg);
if(length == 0) {
cerr << "Empty file was opened for reading: " << filename <<endl;
cerr.flush();
exit(EXIT_FAILURE);
}
} catch (fstream::failure e) {
cerr << "Unable to open file for reading: error = " << strerror(errno) << ", file = " << filename << endl;
cerr.flush();
exit(EXIT_FAILURE);
}
}