-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 9.1 (Completed)
288 lines (249 loc) · 8.12 KB
/
Exercise 9.1 (Completed)
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
1. (Practice) Write declaration and open statements that link the following external filenames
to their corresponding internal filenames. All files are text-based.
External Filename InternalFilename Mode
coba.mem memo output
book.let letter output
coupons.bnd coups append
yield.bnd yield append
prices.dat priFile input
rates.dat rates input
fstream memo;
memo.open("coba.mem" , ios::out);
fstream letter;
letter.open("book.let", ios::out);
fstream coups;
coups.open("coupons.bnd", ios::app);
fstream yield;
yield.open("yield.bnd", ios::app);
fstream priFile;
priFile.open("prices.dat", ios::in);
fstream rates;
rates.open("rates.dat", ios::in);
2. (Practice) a. Write a set of two statements that declares the following objects as ifstream
objects and then opens them as text input files: inData.txt, prices.txt, coupons.dat,
and exper.dat.
ifstream inFile;
inFile.open("inData.txt");
ifstream inFile;
inFile.open("prices.txt");
ifstream inFile;
inFile.open("coupons.dat");
ifstream inFile;
inFile.open("exper.dat");
b. Rewrite the two statements for Exercise 2a, using a single statement.
ifstream inFile("inData.txt");
ifstream inFile("prices.txt");
ifstream inFile("coupons.dat");
ifstream inFile("exper.dat");
3. (Practice) a. Write a set of two statements declaring the following objects as ofstream objects
and then opening them as text output files: outDate.txt, rates.txt, distance.txt, and
file2.txt.
ofstream outFile;
outFile.open("outDate.txt");
ofstream outFile;
outFile.open("rates.txt");
ofstream outFile;
outFile.open("distance.txt");
ofstream outFile;
outFile.open("file2.txt");
b. Rewrite the two statements for Exercise 3a, using a single statement.
ofstream outFile("outDate.txt");
ofstream outFile("rates.txt");
ofstream outFile("distance.txt");
ofstream outFile("file2.txt");
4. (Practice) Enter and run Program 9.1 on your computer.
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
using namespace std;
int main()
{
ifstream inFile;
inFile.open("prices.dat"); // open the file with the
// external name prices.dat
if (inFile.fail()) // check for a successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
system("PAUSE");
exit(1);
}
cout << "\nThe file has been successfully opened for reading."
<< endl;
// statements to read data from the file are placed here
system("PAUSE");
return 0;
}
5. (Practice) Enter and run Program 9.2 on your computer.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
char response;
inFile.open("prices.dat"); // attempt to open the file for input
if (!inFile.fail()) // if it doesn't fail, the file exists
{
cout << "A file by the name prices.dat exists.\n"
<< "Do you want to continue and overwrite it\n"
<< " with the new data(y or n) : ";
cin >> response;
if (tolower(response) == 'n')
{
cout << "The existing file will not be overwritten." << endl;
exit(1); //terminate program execution
}
}
outFile.open("prices.dat"); // now open the file for writing
if (inFile.fail()) // check for a successful open
{
cout << "\nThe file was not successfully opened" << endl;
system("PAUSE");
exit(1);
}
cout << "The file has been successfully opened for output." << endl;
return 0;
}
6. (Practice) a. Enter and run Program 9.3a on your computer.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
using namespace std;
int main()
{
string filename = "prices.dat"; // create and initialize a string object
// with the filename at the top of the
// main() function
ifstream inFile;
inFile.open(filename.c_str()); // open the file
if (inFile.fail()) // check for successful open
{
cout << "\nThe file named " << filename
<< " was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
system("PAUSE");
exit(1);
}
cout << "\nThe file has been successfully opened for reading.\n";
system("PAUSE");
return 0;
}
b. Add a close() method to Program 9.3a, and then run the program.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
using namespace std;
int main()
{
string filename = "prices.dat"; // create and initialize a string object
// with the filename at the top of the
// main() function
ifstream inFile;
inFile.open(filename.c_str()); // open the file
inFile.close();
if (inFile.fail()) // check for successful open
{
cout << "\nThe file named " << filename
<< " was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
system("PAUSE");
exit(1);
}
cout << "\nThe file has been successfully opened for reading.\n";
system("PAUSE");
return 0;
}
7. (Practice) a. Enter and run Program 9.3b on your computer.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
using namespace std;
int main()
{
string filename; // declare a string object with no initialization
ifstream inFile;
cout << "Please enter the name of the file you wish to open : ";
cin >> filename;
inFile.open(filename.c_str()); // open the file
if (inFile.fail()) // check for successful open
{
cout << "\nThe file named " << filename
<< " was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
cout << "\nThe file has been successfully opened for reading.\n";
return 0;
}
b. Add a close() method to Program 9.3b, and then run the program.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
using namespace std;
int main()
{
string filename; // declare a string object with no initialization
ifstream inFile;
cout << "Please enter the name of the file you wish to open : ";
cin >> filename;
inFile.open(filename.c_str()); // open the file
inFile.close();
if (inFile.fail()) // check for successful open
{
cout << "\nThe file named " << filename
<< " was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
cout << "\nThe file has been successfully opened for reading.\n";
return 0;
}
8. (Practice) Using the reference manuals provided with your computer’s OS, determine the following:
a. The maximum number of characters the computer can use to name a file for storage.
255 characters is the max number of character.
b. The maximum number of data files that can be open at the same time.
512 is the max number of data files that can be open at the same time.
9. (Practice) Is calling a saved C++ program a file appropriate? Why or why not?
Yes, because everything on your computer is contained within a file.
10. (Practice) a. Write declaration and open statements to link the following external filenames
to their corresponding internal filenames. Use only ifstream and ofstream objects.
External Filename Internal Filename Mode
coba.mem memo binary and output
coupons.bnd coups binary and append
prices.dat priFile binary and input
ofstream memo;
memo.open("coba.mem");
ifstream coups;
coups.open("coupons.bnd");
ifstream priFile;
priFile.open("prices.dat");
b. Redo Exercise 10a, using only fstream objects.
fstream memo;
memo.open("coba.mem", ios:: out | ios::binary);
fstream coups;
coups.open("coupons.bnd", ios::app | ios::binary);
fstream priFile;
coups.open("prices.dat", ios::in | ios::binary);
c. Write close() statements for each file opened in Exercise 10a.
ofstream memo;
memo.open("coba.mem");
memo.close();
ifstream coups;
coups.open("coupons.bnd");
coups.close();
ifstream priFile;
priFile.open("prices.dat");
coups.close();