-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCounty.cpp
95 lines (84 loc) · 2.2 KB
/
County.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
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
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include "County.h"
using namespace std;
const int MINCODE = 0;
const int MAXCODE = 67;
const int LENGTH = 12;
County::County() : _minCode(MINCODE), _maxCode(MAXCODE), _length(LENGTH)
{
char INFILE[] = "county.db";
ifstream inputFile;
inputFile.open(INFILE);
if(!inputFile)
{
cerr << "Problem opening input file: " << INFILE << endl;
}
else
{
inputFile.seekg(0);
inputFile.read((char *) & _numberCountys, sizeof(_numberCountys));
_pCountys = new char[_numberCountys * _length];
inputFile.seekg(_length);
inputFile.read(_pCountys, _numberCountys * _length);
inputFile.close();
_pCounty = new char[_length + 1];
}
}
County::~County()
{
delete[] _pCountys;
delete[] _pCounty;
}
char * County::GetCounty(int inCountyCode) const
{
_pCounty[_length] = '\0';
memset(_pCounty, '*', _length);
if(inCountyCode < _minCode || inCountyCode > _maxCode)
return _pCounty;
strncpy(_pCounty, _pCountys + ((inCountyCode - _minCode)*_length), _length);
return _pCounty;
}
void County::DisplayCountys() const
{
const int NUMBERROWS = 17;
const int COLUMNWIDTH = 24;
const int BUFFERWIDTH = 90;
const int codeLength = 2;
char outputBuffer[NUMBERROWS][BUFFERWIDTH];
char singleData[_length + 1];
int columnPosition;
int currentColumn;
int currentRow;
int i;
if(_numberCountys <= 0)
{
cerr << "Error! No Counties to Display!" << endl;
return;
}
currentRow = 0;
currentColumn = 0;
memset(outputBuffer, 0x20, sizeof(outputBuffer));
for(i = 0; i < _numberCountys; i++)
{
columnPosition = currentColumn * COLUMNWIDTH;
snprintf(outputBuffer[currentRow] + columnPosition, codeLength + 1, "%02d", i + _minCode);
outputBuffer[currentRow][columnPosition + codeLength] = ' ';
strncpy(singleData, GetCounty(i + _minCode), _length);
strncpy(outputBuffer[currentRow] + (columnPosition + (codeLength + 1)),singleData, _length);
++currentRow;
if (currentRow >= NUMBERROWS)
{
currentRow = 0;
currentColumn++;
continue;
}
}
for(i = 0; i < NUMBERROWS; i++)
{
cout.write(outputBuffer[i], BUFFERWIDTH);
cout << "\n";
}
}