Upload
hue
View
27
Download
0
Embed Size (px)
DESCRIPTION
Object-Oriented Programming Using C++. CLASS 22 File Processing. Objectives. Create, read, write and update files. Creating a Sequential Access File. ios. istream. ostream. iostream. ofstream. ifstream. fstream. Fig. 14.3, Pg. 761. - PowerPoint PPT Presentation
Citation preview
1IDLOOPC1998.
Object-Oriented Programming
Using C++
CLASS 22File Processing
2IDLOOPC1998.
• Create, read, write and update files
Ob
ject
ives
3IDLOOPC1998.
Creating a Sequential Access File
ios
istream ostream
iostreamifstream ofstream
fstream Fig. 14.3, Pg. 761
4IDLOOPC1998.
ofstream outClientFile(“clients.dat”,
ios::out);if (!outClientFile) {cerr<<“File could not be opened”
<< endl;exit (1); // prototype in stdlib.h
}Fig. 14.4, Pg. 762
5IDLOOPC1998.
cout << “Enter the account, name,and balance.” << endl;<< “Enter EOF to end input.”<< endl << “? ”;
int account;char name[30];float balance;while(cin>>account>>name>>balance){outClientFile << account << ‘ ‘<< name
<< ‘ ‘ << balance << endl;cout << “? ”;}return 0; }
Fig. 14.4, Pg. 762
6IDLOOPC1998.
ofstream outclientFile(“clients.dat”, ios::out);
ofstream outclientFile;
outclientFile.open(“clients.dat”, ios::out);
7IDLOOPC1998.
Opening Modes for Files
• ios::app• Write all output to the end of the
file• ios::ate
• Move to the end of the original file when file is opened. Enable data to be written anywhere in the file
Fig. 14.5, Pg. 763
8IDLOOPC1998.
Opening Modes for Files (Cont’d)
• ios::in• Open a file for input.
• ios::out• Open a file for output.
• ios::trunc• Discard the file’s contents if it
exists (this is also the default action for ios::out) Fig. 14.5, Pg. 763
9IDLOOPC1998.
Opening Modes for Files (Cont’d)
• ios::nocreate• If the file does not exist, the
open operation fails.• ios::noreplace
• If the file exists, the open operation fails.
Fig. 14.5, Pg. 763
10IDLOOPC1998.
Reading Data from a Sequential Access File
11IDLOOPC1998.
int main(){ ifstream (inClientFile(“clients.dat”, ios::in); if (!inClientFile) { cerr << “File could not be opened\n”; exit(1); } int account; char name[30]; double balance; cout << “Account” << setw(13) << “Balance\n”; while (inClientFile >> account >> name >> balance) outputLine(account,name,balance); } void outputLine(int acct, char * name, double bal) { cout << acct << setw(13) << name << bal << endl;
}
12IDLOOPC1998.
Read sequentially from a file, selectively displaying records
13IDLOOPC1998.
enum RequestType { ZERO_BALANCE =1, CREDIT_BALANCE, DEBIT_BALANCE, END);
int main(){ ifstream (inClientFile(“clients.dat”, ios::in); if (!inClientFile) { cerr << “File could not be opened\n”; exit(1); } int request, account; char name[30]; double balance; cout << “Enter request\n”; cout << “1 List accounts with zero balances\n”; cout << “2 List accounts with credit balances\n”; cout << “4 – End of run”; request = getRequest();
14IDLOOPC1998.
while ( request != END) { switch (request) { case: ZERO_BALANCE; cout << “\nAccounts with zero” << “ balances:\n”; break; case: CREDIT_BALANCE: cout << “\nAccounts with credit” << “balances\n”; }// end of switch inClientFile >> account >> name >> balance; while (!inClientFile.eof()) { if (shouldDisplay (request,balance)) outputLine(account,name,balance); inClientFile >> account >> name >> balance; }
15IDLOOPC1998.
inClientFile.clear(); inClientFile.seekg(0); request = getRequest();} cout << “End of run”; return 0;}
16IDLOOPC1998.
void outputLine(int acct, char * name, double bal) { cout << acct << setw(13) << name << bal << endl; }bool shouldDisplay(int type, double balance){ if (type == CREDIT_BALANCE && balance < 0) return true;
if (type == ZERO_BALANCE && balance == 0) return true;return false;
}
17IDLOOPC1998.
int getRequest() { int request; do { cin >> request; } while ( request < ZERO_BALANCE && request > END}; }
18IDLOOPC1998.
Creating a Random Access File
AdamsJonesSmith
19IDLOOPC1998.
Creating a Random Access File
AdamsJonesSmith
add Davis
20IDLOOPC1998.
Creating a Random Access File
AdamsJonesSmith
AdamsDavisJonesSmith
add Davis
21IDLOOPC1998.
Creating a Random Access File
Adamsadd Adams
22IDLOOPC1998.
Creating a Random Access File
Adams
Smith
add Adamsadd Smith
23IDLOOPC1998.
Creating a Random Access File
AdamsDavis
Smith
add Adamsadd Smithadd Davis
24IDLOOPC1998.
Creating a Random Access File
AdamsDavisJonesSmith
add Adamsadd Smithadd Davisadd Jones
25IDLOOPC1998.
File Processing and String Stream I/O
Fig. 14.11Page 774
26IDLOOPC1998.
struct clientData
{ int accountNumber; char lastName[15]; char firstName[10]; double balance;
} ;
#include “clntdata.h” // above structure int main() { ofstream outCredit(“credit.dat”, ios::binary); if (!outCredit) { cerr< “File could not be opened”; exit(1); }
27IDLOOPC1998.
clientData blankClient = { 0, “”, “”, 0.0);for (int I = 0; I < 100; I++) outCredit.write((char *) (&blankClient), sizeof( clientData));
return 0;
}
28IDLOOPC1998.
File Processing and String Stream I/O
Fig. 14.12
Page 775-776
29IDLOOPC1998.
#include “clntdata.h” // above structure int main() { ofstream outCredit(“credit.dat”, ios::binary); if (!outCredit) { cerr< “File could not be opened”; exit(1); } cout << “enter account number” << “(1 to 100, 0 to end input)\n”; clientData client; cin >> client.accountNumber;
30IDLOOPC1998.
while (client.accountNumber > 0 && client.accountNumber <=100) { cout << “enter lastname, firstname,” << “balance\n”; cin >> client.lastName >> client.firstName >> client.balance; outCredit.seekp(client.accountNumber –1)* sizeof(clientData)); outCredit.write( (char *) (&client), sizeof (clientData)); cout << “enter account number”; cin >> client.accountNumber; }
return 0; }
31IDLOOPC1998.
Q & A