CMS 3D CMS Logo

binary_ifstream.cc
Go to the documentation of this file.
1 #include "binary_ifstream.h"
2 
3 #include <cstdio>
4 #include <iostream>
5 
7 
9 {
10  init (name);
11 }
12 
14 {
15  init (name.c_str());
16 }
17 
18 void binary_ifstream::init( const char* name)
19 {
20  file_ = fopen( name, "rb");
21  if (file_ == nullptr) {
22  std::cout << "file " << name << " cannot be opened for reading"
23  << std::endl;
24  throw binary_ifstream_error();
25  }
26 }
27 
29 {
30  close();
31 }
33 {
34  if (file_ != nullptr) fclose( file_);
35  file_ = nullptr;
36 }
37 
39  n = static_cast<char>(fgetc(file_));
40  return *this;
41 }
42 
44  n = static_cast<unsigned char>(fgetc(file_));
45  return *this;
46 }
47 
49  fread( &n, sizeof(n), 1, file_); return *this;}
51  fread( &n, sizeof(n), 1, file_); return *this;}
53  fread( &n, sizeof(n), 1, file_); return *this;}
55  fread( &n, sizeof(n), 1, file_); return *this;}
56 
58  fread( &n, sizeof(n), 1, file_); return *this;}
60  fread( &n, sizeof(n), 1, file_); return *this;}
61 
63  fread( &n, sizeof(n), 1, file_); return *this;}
65  fread( &n, sizeof(n), 1, file_); return *this;}
66 
68  n = static_cast<bool>(fgetc(file_));
69  return *this;
70 }
71 
73  unsigned int nchar;
74  (*this) >> nchar;
75  char* tmp = new char[nchar+1];
76  unsigned int nread = fread( tmp, 1, nchar, file_);
77  if (nread != nchar) std::cout << "binary_ifstream error: read less then expected " << std::endl;
78  n.assign( tmp, nread);
79  delete[] tmp;
80  return *this;
81 }
82 
83 bool binary_ifstream::good() const
84 {
85  return !bad() && !eof();
86 }
87 
89 {
90  return feof( file_);
91 }
92 
94 {
95  return file_ == nullptr || ferror( file_) != 0;
96 }
97 
98 // don't know the difference between fail() and bad() (yet)
99 bool binary_ifstream::bad() const {return fail();}
100 
101 bool binary_ifstream::operator!() const {return fail() || bad() || eof();}
102 
103 //binary_ifstream::operator bool() const {return !fail() && !bad();}
104 
106  return good();
107 }
bool bad() const
bool good() const
stream state checking
#define nullptr
bool eof() const
bool operator!() const
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
binary_ifstream(const char *name)
binary_ifstream & operator>>(char &n)
void init(const char *name)
bool fail() const