ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
binstream.h
Go to the documentation of this file.
1#ifndef RWSTREAM_H
2#define RWSTREAM_H
3
4#include <stdio.h>
5#include <cstdlib>
6#include <complex>
7#include <iostream>
8
9
15{
16 public:
18 fileptr=NULL; //we should use NULL (not nullptr) here because FILE use NULL.
19 };
20 Binstream(const std::string,const char*);
21 ~Binstream();
22 FILE* fileptr;
23 void close();
24 void open(const std::string,const char*);
25 bool operator!() const;
26 operator bool() const;
27
28 template<class T>
29 Binstream& operator>>( T& data);
30
31 template<class T>
32 Binstream& operator<<(const T& data);
33
34 template<class T>
35 Binstream& read(T* data,const int n);
36
37 template<class T>
38 Binstream& write(const T* data,const int n);
39
40};
41
42// read a data from file
43template<class T>
45{
46 const int size=sizeof(T);
47 size_t ch = fread(&data,size,1,this->fileptr);
48 if(ch<1)
49 {
50 std::cout<<"Error in Binstream: Some data didn't be read."<<std::endl;
51 std::cout<<"Please make you are using op: \"r\""<<std::endl;
52 exit(0);
53 }
54 return *this;
55}
56
57// write a data into file
58template<class T>
60{
61 const int size=sizeof(T);
62 fwrite(&data,size,1,this->fileptr);
63 return *this;
64}
65
66//read an array of data
67template<class T>
68Binstream& Binstream::read(T* data, const int n)
69{
70 const int size=sizeof(T);
71 size_t ch = fread(data,size,n,this->fileptr);
72 if(ch<n)
73 {
74 std::cout<<"Error in Binstream: Some dynamic memory didn't be read."<<std::endl;
75 std::cout<<"Please make you are using op: \"r\""<<std::endl;
76 exit(0);
77 }
78 return *this;
79}
80
81//write an array of data
82template<class T>
83Binstream& Binstream::write(const T* data, const int n)
84{
85 const int size=sizeof(T);
86 fwrite(data,size,n,this->fileptr);
87 return *this;
88}
89
90
91
92/*//for dynamic memory
93//malloc_usable_size has problem!
94template<class T>
95Binstream& operator<<(Binstream& wstream,T* &data)
96{
97 int size=sizeof(T);
98 int n=malloc_usable_size(data)/sizeof(T);
99 fwrite(data,size,n,wstream.fileptr);
100 return wstream;
101}
102
103
104//for dynamic memory
105template<class T>
106Binstream& operator>>(Binstream& rstream,T* &data)
107{
108 int size=sizeof(T);
109 int n=malloc_usable_size(data)/sizeof(T);
110 std::cout<<malloc_usable_size(data)<<' '<<sizeof(T)<<' '<<n<<std::endl;
111 size_t ch;
112 ch=fread(data,size,n,rstream.fileptr);
113 if(ch<n)
114 {
115 std::cout<<"Error in Binstream: Some dynamic memory didn't be read."<<std::endl;
116 exit(0);
117 }
118 return rstream;
119}
120*/
121
122#endif
123
A stream to read or write binary data.
Definition binstream.h:15
Binstream & write(const T *data, const int n)
Definition binstream.h:83
void open(const std::string, const char *)
Definition binstream.cpp:31
Binstream & operator>>(T &data)
Definition binstream.h:44
bool operator!() const
Definition binstream.cpp:38
~Binstream()
Definition binstream.cpp:17
Binstream & read(T *data, const int n)
Definition binstream.h:68
FILE * fileptr
Definition binstream.h:22
Binstream & operator<<(const T &data)
Definition binstream.h:59
void close()
Definition binstream.cpp:23
Binstream()
Definition binstream.h:17
#define T
Definition exp.cpp:237