310 likes | 436 Views
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.
E N D
Object-Oriented Programming Using C++ CLASS 22File 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
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
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
ofstream outclientFile(“clients.dat”, ios::out);ofstream outclientFile;outclientFile.open(“clients.dat”, ios::out);
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
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
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
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; }
Read sequentially from a file, selectively displaying records
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();
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; }
inClientFile.clear(); inClientFile.seekg(0); request = getRequest();} cout << “End of run”; return 0;}
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; }
int getRequest() { int request; do { cin >> request; } while ( request < ZERO_BALANCE && request > END}; }
Creating a Random Access File Adams Jones Smith
Creating a Random Access File Adams Jones Smith add Davis
Creating a Random Access File Adams Davis Jones Smith Adams Jones Smith add Davis
Creating a Random Access File add Adams Adams
Creating a Random Access File add Adams add Smith Adams Smith
Creating a Random Access File add Adams add Smith add Davis Adams Davis Smith
Creating a Random Access File add Adams add Smith add Davis add Jones Adams Davis Jones Smith
File Processing and String Stream I/O Fig. 14.11 Page 774
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); }
clientData blankClient = { 0, “”, “”, 0.0);for (int I = 0; I < 100; I++) outCredit.write((char *) (&blankClient), sizeof( clientData)); return 0; }
File Processing and String Stream I/O Fig. 14.12 Page 775-776
#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;
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; }