1 / 31

Object-Oriented Programming Using C++

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.

hue
Download Presentation

Object-Oriented Programming Using C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming Using C++ CLASS 22File Processing

  2. Objectives • Create, read, write and update files

  3. Creating a Sequential Access File ios istream ostream iostream ofstream ifstream fstream Fig. 14.3, Pg. 761

  4. 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

  5. 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

  6. ofstream outclientFile(“clients.dat”, ios::out);ofstream outclientFile;outclientFile.open(“clients.dat”, ios::out);

  7. 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

  8. 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

  9. 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

  10. Reading Data from a Sequential Access File

  11. 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; }

  12. Read sequentially from a file, selectively displaying records

  13. 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();

  14. 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; }

  15. inClientFile.clear(); inClientFile.seekg(0); request = getRequest();} cout << “End of run”; return 0;}

  16. 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; }

  17. int getRequest() { int request; do { cin >> request; } while ( request < ZERO_BALANCE && request > END}; }

  18. Creating a Random Access File Adams Jones Smith

  19. Creating a Random Access File Adams Jones Smith add Davis

  20. Creating a Random Access File Adams Davis Jones Smith Adams Jones Smith add Davis

  21. Creating a Random Access File add Adams Adams

  22. Creating a Random Access File add Adams add Smith Adams Smith

  23. Creating a Random Access File add Adams add Smith add Davis Adams Davis Smith

  24. Creating a Random Access File add Adams add Smith add Davis add Jones Adams Davis Jones Smith

  25. File Processing and String Stream I/O Fig. 14.11 Page 774

  26. 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); }

  27. clientData blankClient = { 0, “”, “”, 0.0);for (int I = 0; I < 100; I++) outCredit.write((char *) (&blankClient), sizeof( clientData)); return 0; }

  28. File Processing and String Stream I/O Fig. 14.12 Page 775-776

  29. #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;

  30. 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; }

  31. Q & A

More Related