-----------------------------------------------------------------------------------
文件打开方式
说明
-----------------------------------------------------------------------------------
ios::app 将所有输出写入文件末尾
ios::ate
打开文件以便输出,井移到文件末尾(通常用于添加数据)数据可以写入
文件中的任何地方
ios::in 打开文件以便输入
ios::out 打开文件以便输出
ios::trunc 删除文件现有内容(是ios::out的默认操作)
ios::nocreate
如果文件不存在,则文件打开失败
ios::noreplace
如果文件存在,则文件打开失败
-----------------------------------------------------------------------------------
#ifndef CLNTDATA_H
5 #define CLNTDATA_H
6
7 struct clientData {
8
int accountNumber;
9 char lastName[15];
10 char firstName[10];
11
float balance;
12 } ;
13
14 #endif
6 #include
7 #include
8 #include
9 #include
10 #include
"clntdata.h"
11
12 int enterChoice();
13 void textFile( fstream&
);
14 void updateRecord( fstream& );
15 void newRecord( fstream&
);
16 void deleteRecord( fstream& );
17 void outputLine( ostream&,
const clientData & );
18 int getAccount( const char * );
19
20 enum
Choices { TEXTFILE = 1, UPDATE, NEW, DELETE, END } ;
21
22 int
main()
23 {
24 fstream inOutCredit( "credit.dat", ios::in | ios::out
);
25
26 if ( !inOutCredit ) {
27 cerr << "File could not
be opened." << endl;
28 exit ( 1 );
29 }
3O
31 int
choice;
32
33 while ( ( choice = enetrChoice() ) != END){
34
35
switch ( choice ) {
36 case TEXTFILE:
37 textFile(
inOutCredit );
38 break;
39 case UPDATE:
40
updateReeord( inOutCredit );
41 break;
42 case
NEW:
43 newRecord( inOutCredit );
44 break;
45
case DELETE:
46 deleteRecord( inOutCredit );
47
break;
48 default:
49 cerr << "Incorrect
cholce\n";
50 break;
51 }
52
53
inOutCredit.clear(); // resets end-of-file indicator
54 }
55
56
return 0;
57 }
58
59 // Prompt for and input menu choice
60 int
enterChoice()
61 {
62 cout << "\nEnter your choice" <<
endl
63 << "i - store a formatted text file of
accounts\n"
64 << " called \"print.txt\" for
printing\n"
65 << "2 - update an account\n"
66
<< "3 - add a new account\n"
67 << "4 - delete an
account\n"
68 << "5 - end program\n? ";
69
70 int
menuChoice;
71 cin >> menuChoice;
72 return
menuChoice;
73 }
74
75 // Create formatted text file for printing
76
void textFile( fstream &readFromFile )
77 {
78 ofstream
outPrintFile( "print.txt", ios::out );
79
80 if ( !outPrintFile )
{
81 cerr << "File could not be opened." << endl;
82
exit( 1 );
83 }
84
85 outPrintFile << setiosflags(
ios::left ) << setw( 10 )
86 << "Account" << setw( 16
) << "Last Name" << setw( 11 )
87 << "First Name"
<< resetiosflags( ios::left )
88 << setw( 10 ) <<
"Balance" << endl;
89 readFromFile.seekg(0);
90
91 clientData
client;
92 readFromFile.read( reinterpret_cast( &client
),
93 sizeof( clientData )
);
94
95 while (!readFromFile.eof() ) {
96 if (
client.accountNumber != O )
97 outputLine( outPrintFile, client
);
98
99 readFromFile.read( reinterpret_cast(
&client ),
100 sizeof( clientData ) );
1O1
}
102 }
103
104 // Update an account's balance
105 void
updateRecord( fstream &updateFile )
106 {
107 int account =
getAccount( "Enter account to update" );
108
109 updateFile.seekg( (
account - I ) * sizeof( clientData ) );
110
111 clientData
client;
112 updateFile.read( reinterpret_cast( &client
),
113 sizeof( clientData ) );
114
115 if (
client.accountNumber != 0 ) {
116 outputLine( cout, client );
117
cout << "\nEnter charge (+) or payment (-): ";
118
119 float
transaction; // charge or payment
120 cin >> transaction; //
should validate
121 client.balance += transaction;
122 outputLine(
cout, client );
123 updateFile.seekp((account-1) *
sizeof(clientData));
124 updateFile.write(
125 reinterpret
cast( &client ),
126 sizeof( clientData )
);
127 }
128 else
129 cerr << "Account #" <<
account
130 << " has no information." << endl;
131
}
132
133 // Create and insert new record
134 void newRecord( fstream
&insertInFile )
135 {
136 int account = getAccount( "Enter new
account number" );
137
138 insertInFile.seekg( ( account-1 ) * sizeof(
clientData );
139
14O clientData client;
141 insertInFile.read(
reinterpret cast( &client ),
142 sizeof(
clientData ) );
143
144 if ( client.accountNumber == 0 ) {
145
cout << "Enter lastname, firstname, balance\n? ";
146 cin
>> client.lastName >>
client.firstName
147 >>
client.balance;
148 client.accountNumber = account;
149
insertInFile.seekp( ( account - 1) *
150 sizeof(
clientData ) );
151
insertInFile.write(
152 reinterpret_cast( &client ),
153 sizeof( clientData
) );
154 }
155 else
156 cerr << "Account #" <<
account
157 << " already contains information." <<
endl;
158 }
159
160 // Delete an existing record
161 void
deleteRecord( fstream &deleteFromFile )
162 {
163 int account =
getAccount( "Enter account to delete" );
164
165
deleteFromFile.seekg((account-1) * sizeof( clientData ) );
166
167
clientData client;
168 deleteFromFile.read( reinterpret_cast
&client ),
169 sizeof( clientData ) );
170
171
if ( client.accountNumber != 0 ) {
172 clientData blankClient = { 0, "",
"", 0.0 };
173
174 deleteFromFile.seekp( ( account - 1)
*
175 sizeof( clientData ) );
176
deleteFromFile.write(
177 reinterpret cast(
&blankClient ),
178 sizeof( clientData ) );
179 cout
<< "Account #" << account << " deleted." <<
endl;
180 }
181 else
182 cerr << "Account #" <<
account << " is empty." << endl;
183 }
184
185 // output a
line of client infomation
186 void outputLine( ostream &output, const
clientData &c )
187 {
188 output << setiosflags( ios::left )
<< setw( 10 )
189 << c.accountNumber << setw( 16 )
<< c.lastName
190 << setw( 11 } << c.firstNeme
<< setw( 10 )
191 << setprecision( 2 ) <<
resetiosflags( ios::left )
192 << setiosflags( ios::fixed |
ios::showpoint )
193 << c.balance << '\n';
194
}
195
196 // Get an account number from the keyboard
197 int
getAccount( const char *prompt )
198 {
199 int account;
2OO
201
do {
202 cout << prompt << " (1 - 1oo): ";
203 cin
>> account;
204 } while ( account < 1 || account > 100
);
2O5
206 return account;
207 }
阅读(427) | 评论(0) | 转发(0) |