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
|
// BlankConsoleLab.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#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::ifstream;
using std::ofstream;
const int MAX = 100;
int ReadData(ifstream& inFile, ofstream& outFile,int pickUP[],int DropO[],int Traveller[],float Dis[],float TollA[]);
void WriteOutputFile(ofstream& outFile, int pickUP[], int DropO[], int Traveller[], float Dis[], float TollA[],
int counter);
void PrintTotalsAndSummary(ofstream& out, int totalRecords);
int main()
{
int pickUP[MAX];
int dropO[MAX];
int Traveller[MAX];
float Dis[MAX];
float FareA[MAX];
float TollA[MAX];
ifstream inFile;
//"C:\Users\speed\TEMP"
// Notice how this automatically opens the file
ofstream outFile("C:\\Users\\speed\\TEMP\\Apollo17Output.txt");
//C:\\TEMP\\Chap_11_data.txt
inFile.open("C:\\Users\\speed\\TEMP\\Apollo_17.txt");
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 File";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
}
else
{
cout << "Trouble Opening File";
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++)
{
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";
}
|