Ngôn ngữ lập trình C++ - Chương 7: Ra vào dữ liệu

7.1 Giới thiệu

7.2 Dòng – Stream

7.2.2 Các file header thưviện iostream

7.2.3 Các đối tượng và các lớp I/O

7.3 Xuất theo dòng

7.3.1 Xuất các biến kiểu char*.

7.4 Nhập theo dòng

7.4.1 Các thành viên get và getline

7.4.2 Các thành viên peek, putback, và ignore

7.5 I/O không định dạng sửdụng read, write, và gcount

7.6 Giới thiệu vềcác stream manipulator

7.7 Các trạng thái lỗi của dòng

7.8 Đồng bộmột dòng ra và một dòng vào

pdf93 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1606 | Lượt tải: 2download
Tóm tắt nội dung Ngôn ngữ lập trình C++ - Chương 7: Ra vào dữ liệu, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
eRecord( fstream& );
36 void outputLine( ostream&, const ClientData & );
37 int getAccount( const char * const );
38 
39 enum Choices { PRINT = 1, UPDATE, NEW, DELETE, END };
40 
41 int main()
42 {
43 // open file for reading and writing 
44 fstream inOutCredit( "credit.dat", ios::in | ios::out );
45 
46 // exit program if fstream cannot open file
47 if ( !inOutCredit ) {
48 cerr << "File could not be opened." << endl;
49 exit ( 1 );
50 
51 } // end if
52 
Mở file để đọc và ghi (cần
fstream object).
©2004 Trần Minh Châu.
FOTECH. VNU.
83
fig14_15.cpp
(4 of 14)
53 int choice;
54 
55 // enable user to specify action
56 while ( ( choice = enterChoice() ) != END ) {
57 
58 switch ( choice ) {
59 
60 // create text file from record file
61 case PRINT:
62 printRecord( inOutCredit );
63 break;
64 
65 // update record
66 case UPDATE:
67 updateRecord( inOutCredit );
68 break;
69 
70 // create record
71 case NEW:
72 newRecord( inOutCredit );
73 break;
74 
75 // delete existing record
76 case DELETE:
77 deleteRecord( inOutCredit );
78 break;
79 
Hiện menu và trả về lựa chọn 
người dùng.
©2004 Trần Minh Châu.
FOTECH. VNU.
84
fig14_15.cpp
(5 of 14)
80 // display error if user does not select valid choice
81 default:
82 cerr << "Incorrect choice" << endl;
83 break;
84 
85 } // end switch
86 
87 inOutCredit.clear(); // reset end-of-file indicator
88 
89 } // end while
90 
91 return 0;
92 
93 } // end main
94 
95 // enable user to input menu choice
96 int enterChoice()
97 {
98 // display available options
99 cout << "\nEnter your choice" << endl
100 << "1 - store a formatted text file of accounts" << endl
101 << " called \"print.txt\" for printing" << endl
102 << "2 - update an account" << endl
103 << "3 - add a new account" << endl
104 << "4 - delete an account" << endl
105 << "5 - end program\n? ";
©2004 Trần Minh Châu.
FOTECH. VNU.
85
fig14_15.cpp
(6 of 14)
106 
107 int menuChoice;
108 cin >> menuChoice; // receive choice from user
109 
110 return menuChoice;
111 
112 } // end function enterChoice
113 
114 // create formatted text file for printing
115 void printRecord( fstream &readFromFile )
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 
In ra print.txt. Trước 
tiên, in header của bảng.
©2004 Trần Minh Châu.
FOTECH. VNU.
86
fig14_15.cpp
(7 of 14)
131 // set file-position pointer to beginning of record file
132 readFromFile.seekg( 0 ); 
133 
134 // read first record from record file
135 ClientData client;
136 readFromFile.read( reinterpret_cast( &client ),
137 sizeof( ClientData ) ); 
138 
139 // copy all records from record file into text file
140 while ( !readFromFile.eof() ) {
141 
142 // write single record to text file
143 if ( client.getAccountNumber() != 0 )
144 outputLine( outPrintFile, client );
145 
146 // read next record from record file 
147 readFromFile.read( reinterpret_cast( &client ),
148 sizeof( ClientData ) ); 
149 
150 } // end while
151 
152 } // end function printRecord
153 
Đến đầu file, đọc dữ liệu 
về tài khoản, và in bản 
ghi nếu nó không rỗng.
Lưu ý outputLine
lấy đối số là đối tượng
ostream object (lớp cơ 
sở của ofstream). Nó
có thể ghi ra file (như 
trong trường hợp này)
hoặc cout.
©2004 Trần Minh Châu.
FOTECH. VNU.
87
fig14_15.cpp
(8 of 14)
154 // update balance in record
155 void updateRecord( fstream &updateFile )
156 {
157 // obtain number of account to update
158 int accountNumber = getAccount( "Enter account to update" );
159 
160 // move file-position pointer to correct record in file
161 updateFile.seekg( 
162 ( accountNumber - 1 ) * sizeof( ClientData ) ); 
163 
164 // read first record from file
165 ClientData client;
166 updateFile.read( reinterpret_cast( &client ),
167 sizeof( ClientData ) ); 
168 
169 // update record
170 if ( client.getAccountNumber() != 0 ) {
171 outputLine( cout, client );
172 
173 // request user to specify transaction
174 cout << "\nEnter charge (+) or payment (-): ";
175 double transaction; // charge or payment
176 cin >> transaction;
Đây là fstream (I/O) vì ta phải đọc balance cũ, 
cập nhật nó, và ghi balance mới.
©2004 Trần Minh Châu.
FOTECH. VNU.
88
fig14_15.cpp
(9 of 14)
177 
178 // update record balance
179 double oldBalance = client.getBalance();
180 client.setBalance( oldBalance + transaction );
181 outputLine( cout, client );
182 
183 // move file-position pointer to correct record in file
184 updateFile.seekp( 
185 ( accountNumber - 1 ) * sizeof( ClientData ) ); 
186 
187 // write updated record over old record in file
188 updateFile.write( 
189 reinterpret_cast( &client ),
190 sizeof( ClientData ) ); 
191 
192 } // end if
193 
194 // display error if account does not exist
195 else
196 cerr << "Account #" << accountNumber 
197 << " has no information." << endl;
198 
199 } // end function updateRecord
200 
©2004 Trần Minh Châu.
FOTECH. VNU.
89
fig14_15.cpp
(10 of 14)
201 // create and insert record
202 void newRecord( fstream &insertInFile )
203 {
204 // obtain number of account to create
205 int accountNumber = getAccount( "Enter new account number" );
206 
207 // move file-position pointer to correct record in file
208 insertInFile.seekg( 
209 ( accountNumber - 1 ) * sizeof( ClientData ) );
210
211 // read record from file
212 ClientData client;
213 insertInFile.read( reinterpret_cast( &client ),
214 sizeof( ClientData ) ); 
215
216 // create record, if record does not previously exist
217 if ( client.getAccountNumber() == 0 ) {
218 
219 char lastName[ 15 ];
220 char firstName[ 10 ];
221 double balance;
Đây là fstream vì ta đọc 
thử để xem đã có sẵn một bản 
ghi rỗng hay chưa, nếu chưa, 
ta ghi một bản ghi mới.
©2004 Trần Minh Châu.
FOTECH. VNU.
90
fig14_15.cpp
(11 of 14)
222 
223 // user enters last name, first name and balance
224 cout << "Enter lastname, firstname, balance\n? ";
225 cin >> setw( 15 ) >> lastName;
226 cin >> setw( 10 ) >> firstName;
227 cin >> balance;
228 
229 // use values to populate account values
230 client.setLastName( lastName );
231 client.setFirstName( firstName );
232 client.setBalance( balance );
233 client.setAccountNumber( accountNumber );
234 
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( &client ),
242 sizeof( ClientData ) ); 
243 
244 } // end if
245 
©2004 Trần Minh Châu.
FOTECH. VNU.
91
fig14_15.cpp
(12 of 14)
246 // display error if account previously exists
247 else
248 cerr << "Account #" << accountNumber
249 << " already contains information." << endl;
250 
251 } // end function newRecord
252 
253 // delete an existing record
254 void deleteRecord( fstream &deleteFromFile )
255 {
256 // obtain number of account to delete
257 int accountNumber = getAccount( "Enter account to delete" );
258 
259 // move file-position pointer to correct record in file
260 deleteFromFile.seekg( 
261 ( accountNumber - 1 ) * sizeof( ClientData ) ); 
262 
263 // read record from file
264 ClientData client;
265 deleteFromFile.read( reinterpret_cast( &client ),
266 sizeof( ClientData ) ); 
267 
là fstream vì ta đọc để
kiểm tra xem account có tồn 
tại không. Nếu có, ta ghi dữ
liệu rỗng (xóa nó). Nếu 
không, không cần xóa.
©2004 Trần Minh Châu.
FOTECH. VNU.
92
fig14_15.cpp
(13 of 14)
268 // delete record, if record exists in file
269 if ( client.getAccountNumber() != 0 ) {
270 ClientData blankClient;
271 
272 // move file-position pointer to correct record in file
273 deleteFromFile.seekp( ( accountNumber - 1 ) * 
274 sizeof( ClientData ) ); 
275 
276 // replace existing record with blank record 
277 deleteFromFile.write( 
278 reinterpret_cast( &blankClient ),
279 sizeof( ClientData ) ); 
280 
281 cout << "Account #" << accountNumber << " deleted.\n";
282 
283 } // end if
284 
285 // display error if record does not exist
286 else
287 cerr << "Account #" << accountNumber << " is empty.\n";
288 
289 } // end deleteRecord
290 
©2004 Trần Minh Châu.
FOTECH. VNU.
93
fig14_15.cpp
(14 of 14)
291 // display single record
292 void outputLine( ostream &output, const ClientData &record )
293 {
294 output << left << setw( 10 ) << record.getAccountNumber()
295 << setw( 16 ) << record.getLastName().data()
296 << setw( 11 ) << record.getFirstName().data()
297 << setw( 10 ) << setprecision( 2 ) << right << fixed 
298 << showpoint << record.getBalance() << endl;
299 
300 } // end function outputLine
301 
302 // obtain account-number value from user
303 int getAccount( const char * const prompt )
304 {
305 int accountNumber;
306 
307 // obtain account-number value
308 do {
309 cout << prompt << " (1 - 100): ";
310 cin >> accountNumber;
311 
312 } while ( accountNumber 100 );
313 
314 return accountNumber;
315 
316 } // end function getAccount
outputLine rất mềm dẻo, và có
thể ghi ra ostream object bất kỳ
(chẳng hạn 1 file hoặc cout).

File đính kèm:

  • pdfNgôn ngữ lập trình C++ - Chương 7 Ra vào dữ liệu.pdf