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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
// CST116F2021-Lab8_Schroeder.cpp : This file contains the 'main' function. Program execution begins and ends there.
// ////////////////// 11.7 Exercises SHOWN IN LAB_8_CODE_RUN .txt FILE /////////////////
// ////////////////////// 11.9 Learn By Doing Exercise pp323 ////////////////////////
/*
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stdio.h>
using namespace std;
const int RECORDS = 100;
const int MAX = 4;
int main()
{
int records[RECORDS]{};
int num_records = 0;
ifstream inFile;
FILE* inPtr;
// open the file
fopen_s(&inPtr, "C:\\Users\\Lenovo\\source\\repos\\cst115-lab8-BensProgramma\\CST116F2021-Lab8\\11_13_Average_Data.txt", "r");
int inInt;
// read the file
if (inPtr != 0)
{
if (inPtr != NULL)
{
int j = 0;
while (fscanf_s(inPtr, "%i", &inInt) >= 0)
{
records[num_records] = inInt;
num_records++;
j++;
}
}
}
else
cout << "Error: The File can not be opened";
// close the file
fclose(inPtr);
cout << "Given List:\n";
for (int r = 0; r < num_records; r++)
{
cout << records[r] << " ";
}
// Sort the numbers smallest to largest
int j = 0, k = 0, temp = 0;
for (j = 0; j < num_records; j++)
{
for (k = 0; k < num_records - 1; k++)
{
if (records[k] > records[k + 1])
{
temp = records[k];
records[k] = records[k + 1];
records[k + 1] = temp;
}
}
}
cout << "\nSorted List:\n";
for (int r = 0; r < num_records; r++)
{
cout << records[r] << " ";
}
// Find the median of the list
float median = 0;
// if number of elements are even
if (num_records % 2 == 0)
median = (records[(num_records - 1) / 2] + records[num_records / 2]) / 2.0;
// if number of elements are odd
else
median = records[num_records / 2];
cout << "\nMedian: " << median << "\n";
}
*/
// /////////////////////// 11.13 Debugging Exercises pp 333 - 336 /////////////////////
// ////////////////////// 11.14 Programming Exercise pp336 - 337 //////////////////
// //////////////////// In Class example ////////////////////////////////////
// R-WFiles.cpp using getline
/*
int main()
{
/*
string records[RECORDS][MAX]; //2 dimensional string array
int num_records = 0;
ifstream inFile;
ofstream outFile;
inFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Data.txt"); // open the input file
outFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Report.txt"); // open the output file
if (inFile.is_open() && outFile.is_open()) // LINE BY LINE MANIPULATION
{
while (getline(inFile, records[num_records][0], ' ')) //reads the first item in the file
{
cout << records[num_records][0] << ' '; //print out to screen
outFile << records[num_records][0] << ' '; //print to the output file
getline(inFile, records[num_records][1], ' ');
cout << records[num_records][1] << ' '; //print out to screen
outFile << records[num_records][1] << ' '; //print to the output file
cout << records[num_records][2] << ' '; //print out to screen
outFile << records[num_records][2] << ' '; //print to the output file
getline(inFile, records[num_records][3], ' ');
cout << records[num_records][3] << ' '; //print out to screen
outFile << records[num_records][3] << ' '; //print to the output file
num_records++;
}
}
*/
// R-WFiles.cpp using fscanf
/*
string records[RECORDS][MAX]{}; //2 dimensional string array
int num_records = 0;
char inChar[50];
string inString;
FILE* inPtr; // file pointer
ifstream inFile;
ofstream outFile;
//inFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Data.txt"); // open the input file
fopen_s(&inPtr, "C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Data.txt","r");
outFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Report.txt"); // open the output file
if (inPtr != NULL && outFile.is_open()) //check that there is stuff to read // SAFER WAY TO READ THINGS IN fscan
{
while (fscanf_s(inPtr,"%s", inChar, 49) >=0) //reads the first item in the file, tells how big the space is. left space for null character
{
//inString = inChar; // convert it to a string and inserts the NULL character
records[num_records][0] = inChar;
cout << records[num_records][0] << ' '; //print out to screen
outFile << records[num_records][0] << ' '; //print to the output file
fscanf_s(inPtr,"%s", inChar, 49);
//inString = inChar; // convert it to a string and inserts the NULL character
records[num_records][1] = inChar;
cout << records[num_records][1] << ' '; //print out to screen
outFile << records[num_records][1] << ' '; //print to the output file
fscanf_s(inPtr,"%s", inChar, 49);
//inString = inChar; // convert it to a string and inserts the NULL character
records[num_records][2] = inChar;
cout << records[num_records][2] << ' '; //print out to screen
outFile << records[num_records][2] << ' '; //print to the output file
fscanf_s(inPtr,"%s", inChar, 49);
//inString = inChar; // convert it to a string and inserts the NULL character
records[num_records][3] = inChar;
cout << records[num_records][3] << ' '; //print out to screen
outFile << records[num_records][3] << ' '; //print to the output file
num_records++;
}
}
fclose(inPtr); // close input file
outFile.close(); // close output file
return 0;
}
*/
// R-WFiles.cpp : fscanf_s reading strings
/*
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stdio.h>
using namespace std;
const int RECORDS = 100;
const int MAX = 4;
int main()
{
string records[RECORDS][MAX]{};
int num_records = 0;
ifstream inFile;
ofstream outFile;
outFile.open("C:\\TEMP\\Chap_11_Report.txt");
FILE* inPtr;
char inChar[50]{};
string inString;
if (fopen_s(&inPtr, "C:\\TEMP\\Chap_11_Data.txt", "r") == 0)
{
if (inPtr != NULL && outFile.is_open())
{
while (fscanf_s(inPtr, "%s", inChar, 49) >= 0)
{
//inString = inChar;
records[num_records][0] = inChar;
cout << records[num_records][0];
outFile << records[num_records][0];
fscanf_s(inPtr, "%s", inChar, 49);
//inString = inChar;
records[num_records][1] = inChar;
cout << records[num_records][1];
outFile << records[num_records][1];
fscanf_s(inPtr, "%s", inChar, 49);
//inString = inChar;
records[num_records][2] = inChar;
cout << records[num_records][2];
outFile << records[num_records][2];
fscanf_s(inPtr, "%s", inChar, 49);
//inString = inChar;
records[num_records][3] = inChar;
cout << records[num_records][3];
outFile << records[num_records][3];
num_records++;
}
}
}
outFile.close();
fclose(inPtr);
}
*/
// R-WFiles.cpp : fscanf_s reading ints
/*
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stdio.h>
using namespace std;
const int RECORDS = 100;
const int MAX = 4;
int main()
{
int records[RECORDS]{};
int num_records = 0;
ifstream inFile;
ofstream outFile;
outFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\11_13_MedianReport.txt");
FILE* inPtr;
fopen_s(&inPtr,"C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\11_13_Average_Data.txt","r");
int inInt;
if (inPtr != 0)
{
if (inPtr != NULL && outFile.is_open())
{
int j = 0;
while (fscanf_s(inPtr, "%i", &inInt) >= 0)
{
records[num_records] = inInt;
//cout << records[num_records]<< " ";
outFile << records[num_records]<< " ";
num_records++;
j++;
}
}
}
else
cout << "Error: The File can not be opened";
outFile.close();
fclose(inPtr);
cout << "given list:\n";
for (int r = 0; r < num_records; r++)
{
cout << records[r] << " \n";
}
// Sort the numbers smallest to largest
int j = 0, k = 0, temp = 0;
for (j = 0; j < num_records; j++)
{
for (k = 0; k < num_records - 1; k++)
{
if (records[k] > records[k + 1])
{
temp = records[k];
records[k] = records[k + 1];
records[k + 1] = temp;
}
}
}
cout << "\n sorted list:\n";
for (int r = 0; r < num_records; r++)
{
cout << records[r] << " \n";
}
}
*/
// R-WFiles.cpp : getline ints
/*
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stdio.h>
using namespace std;
const int RECORDS = 100;
const int MAX = 4;
int main()
{
int records[RECORDS]{};
int num_records = 0;
ifstream inFile;
ofstream outFile;
outFile.open("C:\\TEMP\\Chap_11_intReport.txt");
inFile.open("C:\\TEMP\\Chap_11_intData.txt");
string inString;
if (inFile.is_open() && outFile.is_open())
{
while (getline(inFile, inString, ','))
{
records[num_records] = stoi(inString); //string to int
outFile << records[num_records];
cout << records[num_records];
num_records++;
}
}
outFile.close();
inFile.close();
}
*/
// R-WFiles.cpp : cin/cout reading files
/*
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int RECORDS = 100;
const int MAX = 4;
int main()
{
int records[RECORDS];
int num_records = 0;
ifstream inFile("C:\\TEMP\\Chap_11_intData.txt");
ofstream outFile("C:\\TEMP\\Chap_11_intReport.txt");
streambuf* cinbuf = cin.rdbuf();
streambuf* coutbuf = cout.rdbuf();
cin.rdbuf(inFile.rdbuf());
cout.rdbuf(outFile.rdbuf());
while (cin >> records[num_records])
{
cout << records[num_records] << ' ';
num_records++;
}
cin.rdbuf(cinbuf);
cout.rdbuf(coutbuf);
}
*/
|