Beruflich Dokumente
Kultur Dokumente
Outline
14.1
14.2
14.3
14.4
14.5
14.6
14.7
14.8
14.9
14.10
14.11
14.12
Introduction
The Data Hierarchy
Files and Streams
Creating a Sequential-Access File
Reading Data from a Sequential-Access File
Updating Sequential-Access Files
Random-Access Files
Creating a Random-Access File
Writing Data Randomly to a Random-Access File
Reading Data Sequentially from a Random-Access File
Example: A Transaction-Processing Program
Input/Output of Objects
14.1 Introduction
Storage of data
Arrays, variables are temporary
Files are permanent
Magnetic disk, optical disk, tapes
In this chapter
1 or 0
Everything in computer ultimately represented as bits
Cumbersome for humans to use
Character set
Digits, letters, symbols used to represent data
Every character represented by 1's and 0's
Byte: 8 bits
Sally
Tom
Judy
Iris
Randy
Judy
Black
Blue
Green
Orange
Red
Green
Judy
Field
1 Bit
File
Record
...
...
n-1
end-of-file marker
basic_istream
basic_ifstream
basic_ostream
basic_iostream
basic_fstream
2003 Prentice Hall, Inc. All rights reserved.
basic_ofstream
Ofstream outClientFile;
outClientFile.open( "filename", fileOpenMode);
2003 Prentice Hall, Inc. All rights reserved.
10
Description
ios::app
ios::ate
ios::in
ios::out
ios::trunc
ios::binary
11
12
Closing file
outClientFile.close()
Automatically closed when destructor called
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
Outline
fig14_04.cpp
(1 of 2)
std::cout;
std::cin;
std::ios;
std::cerr;
std::endl;
#include <fstream>
using std::ofstream;
#include <cstdlib>
int main()
{
// ofstream constructor opens file
ofstream outClientFile( "clients.dat", ios::out );
// exit program if unable to create file
if ( !outClientFile ) { // overloaded ! operator
cerr << "File could not be opened" << endl;
exit( 1 );
} // end if
13
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Outline
cout << "Enter the account, name, and balance." << endl
<< "Enter end-of-file to end input.\n? ";
int account;
char name[ 30 ];
double balance;
fig14_04.cpp
(2 of 2)
// read account, name and balance from cin, then place in file
while ( cin >> account >> name >> balance ) {
outClientFile << account << ' ' << name << ' ' << balance
<< endl;
cout << "? ";
} // end while
return 0;
} // end main
14
Enter
Enter
? 100
? 200
? 300
? 400
? 500
? ^Z
Outline
fig14_04.cpp
output (1 of 1)
15
Overloaded !
!inClientFile tests if file was opened properly
16
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
Outline
fig14_07.cpp
(1 of 3)
std::cout;
std::cin;
std::ios;
std::cerr;
std::endl;
std::left;
std::right;
std::fixed;
std::showpoint;
#include <fstream>
using std::ifstream;
#include <iomanip>
using std::setw;
using std::setprecision;
#include <cstdlib> // exit prototype
void outputLine( int, const char * const, double );
17
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
int main()
{
// ifstream constructor opens the file
ifstream inClientFile( "clients.dat", ios::in );
// exit program if ifstream could not open file
if ( !inClientFile ) {
cerr << "File could not be opened" << endl;
exit( 1 );
Outline
Open and test file for input.
fig14_07.cpp
(2 of 3)
} // end if
int account;
char name[ 30 ];
double balance;
cout << left << setw( 10 ) << "Account" << setw( 13 )
Read from file until EOF
<< "Name" << "Balance" << endl << fixed << showpoint;
found.
18
54
55
56
57
58
59
60
61
62
63
Outline
fig14_07.cpp
(3 of 3)
fig14_07.cpp
output (1 of 1)
Account
100
200
300
400
500
Name
Jones
Doe
White
Stone
Rich
Balance
24.98
345.67
0.00
-42.16
224.62
19
20
fileObject.seekg(n)
Goes to nth byte from beginning
fileObject.seekg(n, ios::cur)
Goes n bytes forward
fileObject.seekg(y, ios::end)
Goes y bytes back from end
fileObject.seekg(0, ios::cur)
Goes to last byte
seekp similar
2003 Prentice Hall, Inc. All rights reserved.
21
Upcoming example
22
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
Outline
fig14_08.cpp
(1 of 6)
std::cout;
std::cin;
std::ios;
std::cerr;
std::endl;
std::fixed;
std::showpoint;
std::left;
std::right;
#include <fstream>
using std::ifstream;
#include <iomanip>
using std::setw;
using std::setprecision;
#include <cstdlib>
23
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
Outline
fig14_08.cpp
(2 of 6)
int main()
{
// ifstream constructor opens the file
ifstream inClientFile( "clients.dat", ios::in );
// exit program if ifstream could not open file
if ( !inClientFile ) {
cerr << "File could not be opened" << endl;
exit( 1 );
} // end if
int request;
int account;
char name[ 30 ];
double balance;
// get user's request (e.g., zero, credit or debit balance)
request = getRequest();
24
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
Outline
fig14_08.cpp
(3 of 6)
case CREDIT_BALANCE:
cout << "\nAccounts with credit balances:\n";
break;
case DEBIT_BALANCE:
cout << "\nAccounts with debit balances:\n";
break;
} // end switch
25
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
Outline
fig14_08.cpp
(4 of 6)
// display record
if ( shouldDisplay( request, balance ) )
outputLine( account, name, balance );
// read account, name and balance from file
inClientFile >> account >> name >> balance;
} // end inner while
inClientFile.clear();
// reset eof for next input
inClientFile.seekg( 0 ); // move to beginning of file
request = getRequest(); // get additional request from user
} // end outer while
cout << "End of run." << endl;
return 0; // ifstream destructor closes the file
} // end main
26
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
Outline
fig14_08.cpp
(5 of 6)
27
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
Outline
fig14_08.cpp
(6 of 6)
28
Outline
Enter request
1 - List accounts with zero balances
2 - List accounts with credit balances
3 - List accounts with debit balances
4 - End of run
? 1
fig14_08.cpp
output (1 of 2)
29
Outline
Enter request
1 - List accounts with zero balances
2 - List accounts with credit balances
3 - List accounts with debit balances
4 - End of run
? 3
fig14_08.cpp
output (2 of 2)
30
31
32
Instant access
Insert record without destroying other data
Update/delete items without changing other data
33
100
200
300
400
500
byte offsets
}
}
}
}
}
}
100
100
100
100
100
100
bytes
bytes
bytes
bytes
bytes
bytes
34
35
&number is an int *
Convert to const char * with reinterpret_cast
sizeof(number)
Size of number (an int) in bytes
36
Account operations
Update, create new, delete, list all accounts in a file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Outline
clientData.h
(1 of 2)
class ClientData {
public:
// default ClientData constructor
ClientData( int = 0, string = "", string = "", double = 0.0 );
// accessor functions for accountNumber
void setAccountNumber( int );
int getAccountNumber() const;
// accessor functions for lastName
void setLastName( string );
string getLastName() const;
37
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Outline
clientData.h
(2 of 2)
Put limits on the size of the
first and last name.
accountNumber (an int)
and balance (double) are
already of a fixed size.
38
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
Outline
ClientData.cpp
(1 of 4)
using std::string;
#include <cstring>
#include "clientData.h"
// default ClientData constructor
ClientData::ClientData( int accountNumberValue,
string lastNameValue, string firstNameValue,
double balanceValue )
{
setAccountNumber( accountNumberValue );
setLastName( lastNameValue );
setFirstName( firstNameValue );
setBalance( balanceValue );
} // end ClientData constructor
// get account-number value
int ClientData::getAccountNumber() const
{
return accountNumber;
} // end function getAccountNumber
39
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
Outline
ClientData.cpp
(2 of 4)
40
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Outline
ClientData.cpp
(3 of 4)
41
78
79
80
81
82
83
84
85
86
87
88
89
90
Outline
ClientData.cpp
(4 of 4)
42
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
Outline
fig14_12.cpp
(1 of 2)
using std::cerr;
using std::endl;
using std::ios;
#include <fstream>
using std::ofstream;
#include <cstdlib>
#include "clientData.h"
int main()
{
ofstream outCredit( "credit.dat", ios::binary );
43
26
27
28
29
30
31
32
33
34
35
36
37
38
Outline
44
Any record?
(Recordnum - 1) * sizeof(object)
45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Outline
fig14_13.cpp
(1 of 4)
std::cerr;
std::endl;
std::cout;
std::cin;
std::ios;
#include <iomanip>
using std::setw;
#include <fstream>
using std::ofstream;
#include <cstdlib>
#include "clientData.h"
46
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Outline
int main()
{
int accountNumber;
char lastName[ 15 ];
char firstName[ 10 ];
double balance;
fig14_13.cpp
Open file for raw (binary)
(2 of 4)
writing.
47
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Outline
fig14_13.cpp
(3 of 4)
number).
object to
file at specified position.
48
70
71
72
73
74
75
76
77
78
79
Outline
fig14_13.cpp
(4 of 4)
return 0;
} // end main
49
Outline
fig14_13.cpp
output (1 of 1)
balance
balance
balance
50
Upcoming program
Output data from a random-access file
Go through each record sequentially
51
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
Outline
fig14_14.cpp
(1 of 3)
std::cout;
std::endl;
std::ios;
std::cerr;
std::left;
std::right;
std::fixed;
std::showpoint;
#include <iomanip>
using std::setprecision;
using std::setw;
#include <fstream>
using std::ifstream;
using std::ostream;
#include <cstdlib> // exit protoyype
#include "clientData.h" // ClientData class definition
52
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Outline
fig14_14.cpp
(2 of 3)
53
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
Outline
54
Account
29
33
37
88
96
Last Name
Brown
Dunn
Barker
Smith
Stone
First Name
Nancy
Stacey
Doug
Dave
Sam
Outline
Balance
-24.54
314.33
0.00
258.34
34.98
fig14_14.cpp
output (1 of 1)
55
Last Name
Brown
Dunn
Barker
Smith
Stone
First Name
Nancy
Stacey
Doug
Dave
Sam
Balance
-24.54
314.33
0.00
258.34
34.98
0.00
87.99
56
57
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
Outline
fig14_15.cpp
(1 of 14)
std::cout;
std::cerr;
std::cin;
std::endl;
std::ios;
std::left;
std::right;
std::fixed;
std::showpoint;
#include <fstream>
using std::ofstream;
using std::ostream;
using std::fstream;
#include <iomanip>
using std::setw;
using std::setprecision;
#include <cstdlib>
#include "clientData.h"
// exit prototype
// ClientData class definition
58
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Outline
int enterChoice();
void printRecord( fstream& );
void updateRecord( fstream& );
void newRecord( fstream& );
void deleteRecord( fstream& );
void outputLine( ostream&, const ClientData & );
int getAccount( const char * const );
fig14_15.cpp
(2 of 14)
int main()
writing (fstream object
{
needed).
// open file for reading and writing
fstream inOutCredit( "credit.dat", ios::in | ios::out );
// exit program if fstream cannot open file
if ( !inOutCredit ) {
cerr << "File could not be opened." << endl;
exit ( 1 );
} // end if
59
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
Outline
int choice;
// enable user to specify action
while ( ( choice = enterChoice() ) != END ) {
switch ( choice ) {
fig14_15.cpp
(3 of 14)
60
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
Outline
int choice;
// enable user to specify action
while ( ( choice = enterChoice() ) != END ) {
switch ( choice ) {
fig14_15.cpp
(4 of 14)
61
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
Outline
fig14_15.cpp
(5 of 14)
62
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
Outline
int menuChoice;
cin >> menuChoice; // receive choice from user
fig14_15.cpp
(6 of 14)
return menuChoice;
} // end function enterChoice
// create formatted text file for printing
Output to print.txt. First,
void printRecord( fstream &readFromFile )
print the header for the table.
{
// create text file
ofstream outPrintFile( "print.txt", ios::out );
// exit program if ofstream cannot create file
if ( !outPrintFile ) {
cerr << "File could not be created." << endl;
exit( 1 );
} // end if
outPrintFile << left << setw( 10 ) << "Account" << setw( 16 )
<< "Last Name" << setw( 11 ) << "First Name" << right
<< setw( 10 ) << "Balance" << endl;
63
131
// set file-position pointer to beginning of record file
132
readFromFile.seekg( 0 );
133
134
// read first record from record file
Go to front of
file, read
fig14_15.cpp
135
ClientData client;
account data,(7and
print record
of 14)
136
readFromFile.read( reinterpret_cast< char * >( &client ),
if not empty.
137
sizeof( ClientData ) );
138
Note that outputLine
139
// copy all records from record file into text file
takes an ostream object
140
while ( !readFromFile.eof() ) {
(base of ofstream). It can
141
easily print to a file (as in this
142
// write single record to text file
case) or cout.
143
if ( client.getAccountNumber() != 0 )
144
outputLine( outPrintFile, client );
145
146
// read next record from record file
147
readFromFile.read( reinterpret_cast< char * >( &client ),
148
sizeof( ClientData ) );
149
150
} // end while
151
152 } // end function printRecord
153
Outline
64
Outline
fig14_15.cpp
(8 of 14)
65
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
Outline
fig14_15.cpp
(9 of 14)
we
66
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
Outline
fig14_15.cpp
(10 of 14)
67
235
// move file-position pointer to correct record in file
236
insertInFile.seekp( ( accountNumber - 1 ) *
237
sizeof( ClientData ) );
238
239
// insert record in file
240
insertInFile.write(
241
reinterpret_cast< const char * >( &client ),
242
sizeof( ClientData ) );
243
244
} // end if
245
246
// display error if account previously exists
247
else
248
cerr << "Account #" << accountNumber
249
<< " already contains information." << endl;
250
251 } // end function newRecord
252
Outline
fig14_15.cpp
(11 of 14)
68
Outline
69
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
Outline
fig14_15.cpp
(13 of 14)
} // end if
// display error if record does not exist
else
is very
cerr << "Account #" << accountNumber outputLine
<< " is empty.\n";
} // end deleteRecord
70
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
Outline
fig14_15.cpp
(14 of 14)
71
72
One solution