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
|
//Lab 3
//Trevor Bouchillon
#include <iostream>
#include <fstream> // For the files!!!!
#include <iomanip> // For manipulators & formatting options
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::ios;
using std::string;
using std::ifstream;
using std::ofstream;
const int EMPLOYEES = 20;
const int MAX = 21;
string OutFileName = "C:\\TEMP\\smallOut.txt";
string InFileName = "C:\\TEMP\\small.txt";
int ReadData(ifstream& inFile, ofstream& outFile, char name[][MAX], int age[]);
void WriteOutputFile(ofstream& outFile, char name[][MAX], int age[], int counter);
void PrintTotalsAndSummary(ofstream& out, int totalRecords);
int main()
{
char name[EMPLOYEES][MAX];
int age[EMPLOYEES];
int record_counter(0);
ifstream inFile;
// Notice how this automatically opens the file
ofstream outFile(OutFileName); //changed output file to be seperate.
inFile.open(InFileName);
if (inFile.is_open())
{
record_counter = ReadData(inFile, outFile, name, age);
inFile.close();
if (outFile.is_open())
{
WriteOutputFile(outFile, name, age, record_counter);
PrintTotalsAndSummary(outFile, record_counter);
outFile.close();
}
else
{
cout << "Trouble Opening: " << OutFileName;
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
}
else
{
cout << "Trouble Opening: " << InFileName;
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
return 0;
}
int ReadData(ifstream& inFile, ofstream& outFile, char name[][MAX], int age[])
{
int counter = 0;
inFile >> name[counter] >> age[counter]; // Priming Read
while (!inFile.eof())
{
cout << setiosflags(ios::left) << setw(25) << name[counter] << resetiosflags(ios::left) << setw(4) << age[counter] << endl;
counter++;
inFile >> name[counter] >> age[counter];
}
return counter;
}
void WriteOutputFile(ofstream& outFile, char name[][MAX], int age[], int counter)
{
outFile << " Here is the Output File" << endl;
for (int r = 0; r < counter; r++) //made r a less than rather than less than or equal to.
{
outFile << setiosflags(ios::left) << setw(25) << name[r] << setw(4) << resetiosflags(ios::left) << age[r] << endl;
}
}
void PrintTotalsAndSummary(ofstream& outFile, int totalRecords)
{
// To screen
cout << "\n\n\t** Total Records: " << totalRecords << " **\n"
<< "\t\t The End \n";
// To file
outFile << "\n\n\t** Total Records: " << totalRecords << " **\n"
<< "\t\t The End \n";
}
|