include iostream include stdlib include string include cstdlib include

  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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
union UniType
{
int asInt;
float asFloat;
char asChar;
};
typedef enum DataType {INT, FLOAT, CHAR};
struct FieldDscr
{
int size;
int lenght;
int offset;
DataType type;
};
class Data
{
protected:
FieldDscr * fieldDscr;
int fieldQty;
void * data;
int currRec;
int recSize;
int recQty;
public:
//////////////////////////////////////////////////////////////////////
void SetFieldQty(int fq)
{
FieldDscr * newDscr = new FieldDscr[fq];
if (fieldDscr)
{
if (fq < fieldQty)
fieldQty = fq;
memccpy(newDscr, fieldDscr, fieldQty, sizeof(FieldDscr)); //???
delete fieldDscr;
}
fieldQty = fq;
fieldDscr = newDscr;
}
//////////////////////////////////////////////////////////////////////
void SetField(int fn, DataType dt, int ln)
{
fieldDscr->type = dt;
fieldDscr->lenght = ln;
switch (dt)
{
case INT:
fieldDscr[fn].size = sizeof(int);
break;
case FLOAT:
fieldDscr[fn].size = sizeof(float);
break;
case CHAR:
fieldDscr[fn].size = sizeof(char);
break;
}
int offs = 0;
for (int i = 0; i < fieldQty; i++)
{
fieldDscr[i].offset = offs;
offs += fieldDscr[i].size * fieldDscr[i].lenght;
}
recSize = offs;
}
//////////////////////////////////////////////////////////////////////
void SetRecQty(int rq)
{
void * newData = new char[rq * recSize];
if (data)
{
if (rq < recQty)
recQty = rq;
memccpy(newData, data, recQty, recSize); //???
delete (char *) data;
}
recQty = rq;
data = newData;
}
//////////////////////////////////////////////////////////////////////
int SetCurrRec(int cr)
{
if (cr && (cr < recQty))
currRec = cr;
return currRec;
}
//////////////////////////////////////////////////////////////////////
int operator++()
{
if ((currRec + 1) < recQty)
currRec++;
return currRec;
}
//////////////////////////////////////////////////////////////////////
int operator--()
{
if ((currRec - 1) >= 0)
currRec--;
return currRec;
}
//////////////////////////////////////////////////////////////////////
UniType & operator[](int p1)
{
return * ((UniType *) ((char *) data + recSize*currRec + fieldDscr[p1].offset));
}
};
class Table
{
public:
Table()
{
}
Data * operator[] (int p1)
{
return NULL;
}
};
class TStudent
{
public:
uint pkey;
char surname[32];
char name[16];
char father[16];
uint sex;
uint group;
};
void CreateStudentDB()
{
srandomdev();
FILE * fdb = fopen("students", "a");
TStudent sv;
int r = 0;
do
{
memset(&sv, 0, sizeof(sv));
sv.pkey = random();
printf("Surname: ");
gets(sv.surname);
printf("Name: ");
gets(sv.name);
printf("Father name: ");
gets(sv.father);
printf("Male/female: ");
char pysh[16] = "";
gets(pysh);
memset(pysh, 0, 16);
sscanf(pysh,"%d", &sv.sex);
printf("group: ");
gets(pysh);
sscanf(pysh,"%d", &sv.group);
fwrite(&sv, sizeof(sv), 1, fdb);
printf("Rerty? (yes/no): ");
gets(pysh);
r = 0;
while (!r)
if (!strcmp(pysh, "yes")) r = 1;
else
if (!strcmp(pysh, "no")) r = 2;
} while (r!=2);
fclose(fdb);
}
void OpenStudentDB()
{
FILE * fdb = fopen("students", "r");
struct stat studstat;
stat("students", &studstat);
int rqty = int(studstat.st_size) / sizeof(TStudent);
Data Studs;
Studs.SetFieldQty(6);
Studs.SetField(0, INT, 1);
Studs.SetField(1, CHAR, 32);
Studs.SetField(2, CHAR, 16);
Studs.SetField(3, CHAR, 16);
Studs.SetField(4, INT, 1);
Studs.SetField(5, INT, 1);
Studs.SetRecQty(rqty);
printf("\n%d\n", int());
TStudent sv;
for (int i=0; i<rqty; i++)
{
fread(&sv, sizeof(sv), 1, fdb);
Studs[0].asInt = sv.pkey;
memcpy(&Studs[1].asChar, sv.surname, 32);
memcpy(&Studs[2].asChar, sv.name, 16);
memcpy(&Studs[1].asChar, sv.father, 16);
Studs[0].asInt = sv.sex;
Studs[0].asInt = sv.group;
}
printf("PKey: Surname: Name: Father name: Sex: Group:\n");
for (int i=0; i<rqty; i++)
{
printf("%10d %-12s %-12s %-12s %6d %6d\n",
Studs[0].asInt,
&Studs[1].asChar,
&Studs[2].asChar,
&Studs[1].asChar,
Studs[0].asInt,
Studs[0].asInt
);
} while (!feof(fdb));
}
int main(int argc, char *argv[])
{
//Data Students;
//Students[4].asInt = 5;
//CreateStudentDB();
OpenStudentDB();
getchar();
return EXIT_SUCCESS;
}