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

Object-Oriented Programming Using C++

  • 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

Page 1: Object-Oriented  Programming  Using C++

1IDLOOPC1998.

Object-Oriented Programming

Using C++

CLASS 22File Processing

Page 2: Object-Oriented  Programming  Using C++

2IDLOOPC1998.

• Create, read, write and update files

Ob

ject

ives

Page 3: Object-Oriented  Programming  Using C++

3IDLOOPC1998.

Creating a Sequential Access File

ios

istream ostream

iostreamifstream ofstream

fstream Fig. 14.3, Pg. 761

Page 4: Object-Oriented  Programming  Using C++

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

Page 5: Object-Oriented  Programming  Using C++

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

Page 6: Object-Oriented  Programming  Using C++

6IDLOOPC1998.

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

ofstream outclientFile;

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

Page 7: Object-Oriented  Programming  Using C++

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

Page 8: Object-Oriented  Programming  Using C++

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

Page 9: Object-Oriented  Programming  Using C++

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

Page 10: Object-Oriented  Programming  Using C++

10IDLOOPC1998.

Reading Data from a Sequential Access File

Page 11: Object-Oriented  Programming  Using C++

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;

}

Page 12: Object-Oriented  Programming  Using C++

12IDLOOPC1998.

Read sequentially from a file, selectively displaying records

Page 13: Object-Oriented  Programming  Using C++

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

Page 14: Object-Oriented  Programming  Using C++

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

Page 15: Object-Oriented  Programming  Using C++

15IDLOOPC1998.

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

Page 16: Object-Oriented  Programming  Using C++

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;

}

Page 17: Object-Oriented  Programming  Using C++

17IDLOOPC1998.

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

Page 18: Object-Oriented  Programming  Using C++

18IDLOOPC1998.

Creating a Random Access File

AdamsJonesSmith

Page 19: Object-Oriented  Programming  Using C++

19IDLOOPC1998.

Creating a Random Access File

AdamsJonesSmith

add Davis

Page 20: Object-Oriented  Programming  Using C++

20IDLOOPC1998.

Creating a Random Access File

AdamsJonesSmith

AdamsDavisJonesSmith

add Davis

Page 21: Object-Oriented  Programming  Using C++

21IDLOOPC1998.

Creating a Random Access File

Adamsadd Adams

Page 22: Object-Oriented  Programming  Using C++

22IDLOOPC1998.

Creating a Random Access File

Adams

Smith

add Adamsadd Smith

Page 23: Object-Oriented  Programming  Using C++

23IDLOOPC1998.

Creating a Random Access File

AdamsDavis

Smith

add Adamsadd Smithadd Davis

Page 24: Object-Oriented  Programming  Using C++

24IDLOOPC1998.

Creating a Random Access File

AdamsDavisJonesSmith

add Adamsadd Smithadd Davisadd Jones

Page 25: Object-Oriented  Programming  Using C++

25IDLOOPC1998.

File Processing and String Stream I/O

Fig. 14.11Page 774

Page 26: Object-Oriented  Programming  Using C++

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

Page 27: Object-Oriented  Programming  Using C++

27IDLOOPC1998.

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

return 0;

}

Page 28: Object-Oriented  Programming  Using C++

28IDLOOPC1998.

File Processing and String Stream I/O

Fig. 14.12

Page 775-776

Page 29: Object-Oriented  Programming  Using C++

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;

Page 30: Object-Oriented  Programming  Using C++

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

Page 31: Object-Oriented  Programming  Using C++

31IDLOOPC1998.

Q & A