-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatafile.hpp
67 lines (58 loc) · 1.38 KB
/
datafile.hpp
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
65
66
67
template <class T> class DataFile {
public:
vector<T> data;
ofstream file;
ofstream txtfile;
string filename;
long long total_count;
const char * dirname;
long blockcount;
DataFile(const char * dirname,long blockcount,const char * filename)
:
total_count(0),
filename(filename),
dirname(dirname),
blockcount(blockcount)
{
string outputbase(string (dirname) + string(filename));
file.open(string (outputbase + ".bin").c_str());
txtfile.open(string(outputbase+ ".txt").c_str());
}
~DataFile()
{
int count =data.size();
write(count);
file.close();
txtfile.close();
cout << "Closing file "<< dirname << filename << ", wrote " << total_count << endl;
}
void flush()
{
write(data.size());
}
long long count()
{
return total_count;
}
void write(int count)
{
// append the data to the file
file.write((const char*)&data[0], count * sizeof(T));
typename vector<T>::iterator i;
for(i=data.begin();i!=data.end();i++)
{
txtfile << *i << endl;
}
data.clear(); // erase the data
}
void push_back (const T& v){
total_count++;
// cout << "pushing " << filename << " value "<< v << endl;
data.push_back(v);
int count =data.size();
if (count > blocksize)
{
write(count);
}
}
};