-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 9.3 (Completed)
226 lines (196 loc) · 5.37 KB
/
Exercise 9.3 (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
1. (Practice) a. Create a file named test.dat containing the data in the test.dat file used in
Program 9.7. (You can use a text editor or copy the test.dat file from this book’s Web site.)
Done!!
test.dat contains the words "The grade was 92.5".
b. Enter and run Program 9.7 on your computer.
Done!!
2. (Modify) Rewrite Program 9.7 so that the origin for the seekg() method used in the for loop
is the start of the file rather than the end.
for (offset = 0L; offset <= last; offset++)
{
inFile.seekg(offset, ios::beg);
ch = inFile.get();
cout << ch << " : ";
}
inFile.close();
cout << endl;
3. (Modify) Modify Program 9.7 to display an error message if seekg() attempts to reference a
position beyond the end of file.
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
string filename = "test.dat";
char ch;
long offset, last;
ifstream inFile(filename.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists"
<< endl;
exit(1);
}
inFile.seekg(0L, ios::end); // move to the end of the file
last = inFile.tellg(); // save the offset of the last character
for (offset = 0L; offset <= last-1; offset++)
{
inFile.seekg(offset, ios::beg);
ch = inFile.get();
cout << ch << " : ";
if (offset >= last){
cout << "Error" << endl;
}
}
inFile.close();
cout << endl;
system("PAUSE");
return 0;
}
4. (Practice) Write a program that reads and displays every second character in a file named
test.dat.
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
string filename = "test.dat";
char ch;
long offset, last;
ifstream inFile(filename.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists"
<< endl;
exit(1);
}
inFile.seekg(0L, ios::end); // move to the end of the file
last = inFile.tellg(); // save the offset of the last character
for (offset = 0L; offset <= last - 1; offset++)
{
if (offset % 2 == 0){
inFile.seekg(offset, ios::beg);
ch = inFile.get();
cout << ch << " ";
}
}
inFile.close();
cout << endl;
system("PAUSE");
return 0;
}
5. (Practice) Using the seek() and tell() methods, write a function named fileChars()
that returns the total number of characters in a file.
double fileChars(){
string filename = "test.dat";
char ch;
long offset, last;
int total = 0;
ifstream inFile(filename.c_str());
inFile.seekg(0L, ios::end); // move to the end of the file
last = inFile.tellg(); // save the offset of the last character
for (offset = 0L; offset <= last; offset++)
{
inFile.seekg(offset, ios::beg);
ch = inFile.get();
//cout << ch << " ";
total =+ offset;
}
inFile.close();
return total;
}
6. (Practice) a. Write a function named readBytes() that reads and displays n characters starting
from any position in a file. The function should accept three arguments: a file object name,
the offset of the first character to be read, and the number of characters to be read. (Note: The
prototype for readBytes() should be void readBytes(fstream&, long, int).)
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void readBytes(ofstream& fileOut, long value, int n){
string filename = "test.dat";
char ch;
long last;
int total = 0;
ifstream inFile(filename.c_str());
inFile.seekg(0L, ios::end); // move to the end of the file
last = inFile.tellg(); // save the offset of the last character
for (long offset = value; offset <= value+n; offset++)
{
inFile.seekg(offset, ios::beg);
ch = inFile.get();
cout << ch << " ";
}
inFile.close();
return;
}
int main()
{
string filename = "test.dat";
char ch;
long offset, last;
string file = "test1.dat";
ifstream inFile(filename.c_str());
ofstream fileOut(file.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists"
<< endl;
exit(1);
}
readBytes(fileOut, 2L, 8L);
system("PAUSE");
return 0;
}
b. Modify the readBytes() function written in Exercise 6a to store the characters read into
a string or an array. The function should accept the storage address as a fourth argument.
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void readBytes(ofstream& fileOut, long value, int n){
string filename = "test.dat";
char ch;
long last;
int total = 0;
ifstream inFile(filename.c_str());
inFile.seekg(0L, ios::end); // move to the end of the file
last = inFile.tellg(); // save the offset of the last character
for (long offset = value; offset <= value+n; offset++)
{
inFile.seekg(offset, ios::beg);
ch = inFile.get();
cout << ch << " ";
fileOut << ch << " ";
}
inFile.close();
return;
}
int main()
{
string filename = "test.dat";
char ch;
long offset, last;
string file = "test1.dat";
ifstream inFile(filename.c_str());
ofstream fileOut(file.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists"
<< endl;
exit(1);
}
readBytes(fileOut, 2L, 8L);
system("PAUSE");
return 0;
}