-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructure.cpp
70 lines (64 loc) · 1.4 KB
/
structure.cpp
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
68
69
70
#pragma once
#include "structure.h"
Table::Table()
{
tuples.reserve(10000);
attr.reserve(11);
}
void Table::write_data()
{
string file_name = table_name + ".txt";
FILE *p = fopen(file_name.c_str(), "w");
if (!p) {
cout << "Cannot open file.(writing)\n";
return;
}
int tuple_size = tuples.size();
int attr_size = attr.size();
int i, j;
fprintf(p, "%d\n", tuple_size);
fprintf(p, "%d\n", attr_size);
for (i = 0; i < tuple_size; i++)
{
for (j = 0; j < attr_size; j++)
{
fprintf(p, "%s\n", tuples[i].values[j].val.c_str());
//fwrite(tuples[i].values[j].val.c_str(), sizeof(tuples[i].values[j].val.c_str()),1, p);
//fprintf(p, "\n");
}
}
fclose(p);
}
void Table::load_data()
{
string file_name = table_name + ".txt";
FILE *p = fopen(file_name.c_str(), "r");
if (!p) {
cout << "Cannot open file.(reading)\n";
return;
}
int tuple_size = 0, attr_size = 0;
char t[10], a[10];
fgets(t, 10, p);
tuple_size = atoi(t);
//cout << tuple_size << endl;
fgets(a, 10, p);
attr_size = atoi(a);
//cout << attr_size << endl;
int i, j;
for (i = 0; i < tuple_size; i++)
{
Tuple t;
for (j = 0; j < attr_size; j++)
{
char temp[50];
fgets(temp, 45, p);
string str = temp;
Value v(attr[j].type, str.substr(0,str.length() -1 ));
t.values.push_back(v);
}
tuples.push_back(t);
}
fclose(p);
//print_table();
}