Files C++
Files C++
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
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
14.1 Introduction
Storage of data
Arrays, variables are temporary
Files are permanent
Magnetic disk, optical disk, tapes
In this chapter
Create, update, process files
Sequential and random access
Formatted and raw processing
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
Can store a character (char)
Also Unicode for large character sets (wchar_t)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
Sally
Tom
Judy
Iris
Randy
Judy
Black
Blue
Green
Orange
Red
Green
Judy
File
Record
Field
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
...
...
n-1
end-of-file marker
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
basic_istream
basic_ifstream
basic_ostream
basic_iostream
basic_ofstream
basic_fstream
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
10
Description
ios::app
ios::ate
ios::in
ios::out
ios::trunc
ios::binary
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
11
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
12
Closing file
outClientFile.close()
Automatically closed when destructor called
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
13
using
using
using
using
using
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
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
14
cout << "Enter the account, name, and balance." << endl
<< "Enter end-of-file tocin
end is
input.\n?
implicitly";
converted to
int account;
char name[ 30 ];
double balance;
Outline
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
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
Enter
Enter
? 100
? 200
? 300
? 400
? 500
? ^Z
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
15
Outline
fig14_04.cpp
output (1 of 1)
16
Overloaded !
!inClientFile tests if file was opened properly
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
using
using
using
using
using
using
using
using
using
std::cout;
std::cin;
std::ios;
std::cerr;
std::endl;
std::left;
std::right;
std::fixed;
std::showpoint;
17
Outline
fig14_07.cpp
(1 of 3)
#include <fstream>
using std::ifstream;
#include <iomanip>
using std::setw;
using std::setprecision;
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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 );
18
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.
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
54
55
56
57
58
59
60
61
62
63
19
// display single record from file
void outputLine( int account, const char * const name,
double balance )
{
cout << left << setw( 10 ) << account << setw( 13 ) << name
<< setw( 7 ) << setprecision( 2 ) << right << balance
<< endl;
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
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
20
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
21
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.
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
22
Upcoming example
Credit manager program
List accounts with zero balance, credit, and debit
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
using
using
using
using
using
using
using
using
using
std::cout;
std::cin;
std::ios;
std::cerr;
std::endl;
std::fixed;
std::showpoint;
std::left;
std::right;
23
Outline
fig14_08.cpp
(1 of 6)
#include <fstream>
using std::ifstream;
#include <iomanip>
using std::setw;
using std::setprecision;
#include <cstdlib>
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
24
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;
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
25
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
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
26
Outline
fig14_08.cpp
(4 of 6)
// display record
if ( shouldDisplay( request, balance ) )
outputLine( account, name, balance );
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;
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
27
// obtain request from user
int getRequest()
{
int request;
Outline
fig14_08.cpp
(5 of 6)
120
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
28
Outline
fig14_08.cpp
(6 of 6)
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
29
Outline
fig14_08.cpp
output (1 of 2)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
30
Outline
fig14_08.cpp
output (2 of 2)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
31
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
32
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
33
100
200
300
400
500
byte offsets
100
100
100
100
100
100
bytes
bytes
bytes
bytes
bytes
bytes
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
34
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
35
&number is an int *
Convert to const char * with reinterpret_cast
sizeof(number)
Size of number (an int) in bytes
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
36
Problem statement
Credit processing program
Store at most 100 fixed-length records
Record
Account number (key)
First and last name
Balance
Account operations
Update, create new, delete, list all accounts in a file
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using std::string;
37
Outline
clientData.h
(1 of 2)
class ClientData {
public:
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
38
Outline
clientData.h
(2 of 2)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
using std::string;
39
Outline
ClientData.cpp
(1 of 4)
#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;
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
40
// set account-number value
void ClientData::setAccountNumber( int accountNumberValue )
{
accountNumber = accountNumberValue;
Outline
ClientData.cpp
(2 of 4)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
41
} // end function setLastName
// get first-name value
string ClientData::getFirstName() const
{
return firstName;
Outline
ClientData.cpp
(3 of 4)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
78
79
80
81
82
83
84
85
86
87
88
89
90
42
Outline
ClientData.cpp
(4 of 4)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
43
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 );
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
26
27
28
29
30
31
32
33
34
35
36
37
38
44
// create ClientData with no information
ClientData blankClient;
Outline
Create a blank object. Use
write to output the raw data
fig14_12.cpp
to a file (passing a pointer to
(2 of 2)
the object and its size).
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
45
Any record?
(Recordnum - 1) * sizeof(object)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using
using
using
using
using
46
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"
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
47
int main()
{
int accountNumber;
char lastName[ 15 ];
char firstName[ 10 ];
double balance;
Outline
Open file for raw (binary)
fig14_13.cpp
writing.
(2 of 4)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
48
Outline
fig14_13.cpp
(3 of 4)
number).
object
to file at specified position.
69
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
70
71
72
73
74
75
76
77
78
79
49
Outline
fig14_13.cpp
(4 of 4)
return 0;
} // end main
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
50
Outline
balance
fig14_13.cpp
output (1 of 1)
balance
balance
balance
balance
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
51
Upcoming program
Output data from a random-access file
Go through each record sequentially
If no data (accountNumber == 0) then skip
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
using
using
using
using
using
using
using
using
std::cout;
std::endl;
std::ios;
std::cerr;
std::left;
std::right;
std::fixed;
std::showpoint;
52
Outline
fig14_14.cpp
(1 of 3)
#include <iomanip>
using std::setprecision;
using std::setw;
#include <fstream>
using std::ifstream;
using std::ostream;
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
53
Outline
fig14_14.cpp
(2 of 3)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
54
Outline
// display record
if ( client.getAccountNumber() != 0 )
outputLine( cout, client );
return 0;
} // end main
} // end outputLine
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
Account
29
33
37
88
96
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
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
55
Outline
fig14_14.cpp
output (1 of 1)
56
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
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
57
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
58
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
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
59
int enterChoice();
void printRecord( fstream& );
void updateRecord( fstream& );
void newRecord( fstream& );
void deleteRecord( fstream& );
void outputLine( ostream&, const ClientData & );
int getAccount( const char * const );
Outline
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
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
60
53
int choice;
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
switch ( choice ) {
fig14_15.cpp
(3 of 14)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
61
53
int choice;
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
switch ( choice ) {
fig14_15.cpp
(4 of 14)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
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
62
Outline
fig14_15.cpp
(5 of 14)
} // end main
// enable user to input menu choice
int enterChoice()
{
// display available options
cout << "\nEnter your choice" << endl
<< "1 - store a formatted text file of accounts" << endl
<< "
called \"print.txt\" for printing" << endl
<< "2 - update an account" << endl
<< "3 - add a new account" << endl
<< "4 - delete an account" << endl
<< "5 - end program\n? ";
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
106
107
int menuChoice;
108
cin >> menuChoice; // receive choice from user
109
110
return menuChoice;
111
112 } // end function enterChoice
63
Outline
fig14_15.cpp
(6 of 14)
113
114 // create formatted text file for printing
Output to print.txt. First,
115 void printRecord( fstream &readFromFile )
print the header for the table.
116 {
117
// create text file
118
ofstream outPrintFile( "print.txt", ios::out );
119
120
// exit program if ofstream cannot create file
121
if ( !outPrintFile ) {
122
cerr << "File could not be created." << endl;
123
exit( 1 );
124
125
} // end if
126
127
outPrintFile << left << setw( 10 ) << "Account" << setw( 16 )
128
<< "Last Name" << setw( 11 ) << "First Name" << right
129
<< setw( 10 ) << "Balance" << endl;
130
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
131
132
133
134
135
136
137
64
Outline
Go to front of
file, read
fig14_15.cpp
account data,(7and
print record
of 14)
if not empty.
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
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
177
178
179
180
181
182
65
Outline
fig14_15.cpp
(8 of 14)
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
66
Outline
fig14_15.cpp
(9 of 14)
we
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
67
// read record from file
ClientData client;
insertInFile.read( reinterpret_cast< char * >( &client ),
sizeof( ClientData ) );
Outline
fig14_15.cpp
(10 of 14)
234
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
235
236
237
238
239
240
241
242
243
244
68
Outline
fig14_15.cpp
(11 of 14)
} // 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
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
Outline
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
69
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
70
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
299
300 } // end function outputLine
301
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
306
307
// obtain account-number value
308
do {
309
cout << prompt << " (1 - 100): ";
310
cin >> accountNumber;
311
312
} while ( accountNumber < 1 || accountNumber > 100 );
313
314
return accountNumber;
315
316 } // end function getAccount
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.
71
Outline
fig14_15.cpp
(14 of 14)
72
One solution
When writing, output object type code before real object
When reading, read type code
Call proper overloaded function (switch)
Reproduced from the PowerPoints for C++ How to Program, 4/e by Deitel and
Deitel 2003. Reproduced by permission of Pearson Education, Inc.