2000 deitel & associates, inc. all rights reserved. chapter 14 – file processing outline...

40
2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1 Introduction 14.2 The Data Hierarchy 14.3 Files and Streams 14.4 Creating a Sequential Access File 14.5 Reading Data from a Sequential Access File 14.6 Updating Sequential Access Files 14.7 Random Access Files 14.8 Creating a Random Access File 14.9 Writing Data Randomly to a Random Access File 14.10 Reading Data Sequentially from a Random Access File 14.11 Example: A Transaction Processing Program 14.12 Input/Output of Objects

Upload: amberlynn-flowers

Post on 11-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Chapter 14 – File Processing

Outline

14.1 Introduction

14.2 The Data Hierarchy

14.3 Files and Streams

14.4 Creating a Sequential Access File

14.5 Reading Data from a Sequential Access File

14.6 Updating Sequential Access Files

14.7 Random Access Files

14.8 Creating a Random Access File

14.9 Writing Data Randomly to a Random Access File

14.10 Reading Data Sequentially from a Random Access File

14.11 Example: A Transaction Processing Program

14.12 Input/Output of Objects

Page 2: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.1 Introduction

• Data files can be created, updated, and processed by C++ programs – Files are used for permanent storage of large amounts

of data

– Storage of data in variables and arrays is only temporary

Page 3: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.2 The Data Hierarchy

• Bit - smallest data item– value of 0 or 1

• Byte – 8 bits – used to store a character

• Decimal digits, letters, and special symbols

• Field - group of characters conveying meaning – Example: your name

• Record – group of related fields– Represented a struct or a class– Example: In a payroll system, a record for a particular employee

that contained his/her identification number, name, address, etc.

• File – group of related records– Example: payroll file

• Database – group of related files

Page 4: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.2 The Data Hierarchy (II)

• Record key– identifies a record to facilitate the retrieval of specific records from

a file

• Sequential file – records typically sorted by key

1

01001010

Judy

Judy Green

Sally BlackTom BlueJudy GreenIris OrangeRandy Red

File

Record

Field

Byte (ASCII character J)

Bit

Page 5: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.3 Files and Streams

• C++ views each file as a sequence of bytes– File ends with the end-of-file marker

• Stream created when a file is opened

• File processing– Headers <iostream.h> and <fstream.h> – class ifstream - input– class ofstream - output– class fstream - either input or output

Page 6: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.4 Creating a Sequential Access File

• Files are opened by creating objects of stream classes ifstream, ofstream or fstream

• File stream member functions for object file:– file.open(“Filename”, fileOpenMode);– file.close();

• destructor automatically closes file if not explicitly closed

• File open modes:Mode Description

ios::app Write all output to the end of the file.

ios::ate Open a file for output and move to the end of the file (normally used to append data to a file). Data can be written anywhere in the file.

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)

ios::binary Open a file for binary (i.e., non-text) input or output.

Makes a "line of communication" with the object and the file.

Page 7: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1. Load headers

1.1 Initialize ofstream object

1 // Fig. 14.4: fig14_04.cpp

2 // Create a sequential file

3 #include <iostream>

4

5 using std::cout;

6 using std::cin;

7 using std::ios;

8 using std::cerr;

9 using std::endl;

10

11 #include <fstream>

12

13 using std::ofstream;

14

15 #include <cstdlib>

16

17 int main()

18 {

19 // ofstream constructor opens file

20 ofstream outClientFile( "clients.dat", ios::out );

ofstream objects open a file for output (i.e., write to the file). If the file "clients.dat" does not exist, it is created. ios::out is the default for ofstream objects.

Page 8: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

2. Test for file open

2.1 Input values

3. Output to file

Program Output

21

22 if ( !outClientFile ) { // overloaded ! operator

23 cerr << "File could not be opened" << endl;

24 exit( 1 ); // prototype in cstdlib

25 }

26

27 cout << "Enter the account, name, and balance.\n"

28 << "Enter end-of-file to end input.\n? ";

29

30 int account;

31 char name[ 30 ];

32 double balance;

33

34 while ( cin >> account >> name >> balance ) {

35 outClientFile << account << ' ' << name

36 << ' ' << balance << '\n';

37 cout << "? ";

38 }

39

40 return 0; // ofstream destructor closes file

41 }

Enter the account, name, and balance.Enter EOF to end input.? 100 Jones 24.98? 200 Doe 345.67? 300 White 0.00? 400 Stone -42.16? 500 Rich 224.62? ^Z

Overloaded operator! returns true if failbit or badbit are set.

Input sets of data. When end-of-file or bad data is input, cin returns 0 (normally it returns cin), and the while loop ends.

Rather than outputting to cout, we output to outClientFile, which is linked to clients.dat

outClientFile's destructor automatically closes client.dat

Page 9: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.5 Reading Data from a Sequential Access File

• File stream member functions for repositioning file position pointer:– seekg (seek get) for istream and seekp (seek put) for ostream• // position to the nth byte of fileObject

// assumes ios::beg fileObject.seekg( n );

• // position n bytes forward in fileObjectfileObject.seekg( n, ios::cur );

• // position y bytes back from end of fileObjectfileObject.seekg( y, ios::end );

• // position at end of fileObjectfileObject.seekg( 0, ios::end );

– tellg and tellp – return current location of pointerlocation = fileObject.tellg() //returns long

Page 10: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Reading a sequential file

1. Load header

1.1 Function prototype

1 // Fig. 14.7: fig14_07.cpp

2 // Reading and printing a sequential file

3 #include <iostream>

4

5 using std::cout;

6 using std::cin;

7 using std::ios;

8 using std::cerr;

9 using std::endl;

10

11 #include <fstream>

12

13 using std::ifstream;

14

15 #include <iomanip>

16

17 using std::setiosflags;

18 using std::resetiosflags;

19 using std::setw;

20 using std::setprecision;

21

22 #include <cstdlib>

23

24 void outputLine( int, const char * const, double );

25

26 int main()

27 {

28 // ifstream constructor opens the file

Page 11: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.2 Initialize ifstream object

1.3 Test for open

1.4 Initialize variables

2. Format table

3. Output data

3.1 Function definition

29 ifstream inClientFile( "clients.dat", ios::in );

30

31 if ( !inClientFile ) {

32 cerr << "File could not be opened\n";

33 exit( 1 );

34 }

35

36 int account;

37 char name[ 30 ];

38 double balance;

39

40 cout << setiosflags( ios::left ) << setw( 10 ) << "Account"

41 << setw( 13 ) << "Name" << "Balance\n"

42 << setiosflags( ios::fixed | ios::showpoint );

43

44 while ( inClientFile >> account >> name >> balance )

45 outputLine( account, name, balance );

46

47 return 0; // ifstream destructor closes the file

48 }

49

50 void outputLine( int acct, const char * const name, double bal )

51 {

52 cout << setiosflags( ios::left ) << setw( 10 ) << acct

53 << setw( 13 ) << name << setw( 7 ) << setprecision( 2 )

54 << resetiosflags( ios::left )

55 << bal << '\n';

56 }

Opens "clients.dat" for input (i.e., read from file)

Account Name Balance

Reads data, record by record, and puts it into account, name and balance.

Calls outputLine, which formats data and outputs it to the screen.

while loop continues until the end of the file stream, when 0 is returned (instead of inClientFile). The destructor automatically closes clients.dat

Page 12: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

Account Name Balance100 Jones 24.98200 Doe 345.67300 White 0.00400 Stone -42.16500 Rich 224.62

Page 13: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Credit inquiry program

1. Load headers

1.1 Function prototypes

1.2 enumeration

1 // Fig. 14.8: fig14_08.cpp2 // Credit inquiry program3 #include <iostream>45 using std::cout;6 using std::cin;7 using std::ios;8 using std::cerr;9 using std::endl;1011 #include <fstream>1213 using std::ifstream;1415 #include <iomanip>1617 using std::setiosflags;18 using std::resetiosflags;19 using std::setw;20 using std::setprecision;2122 #include <cstdlib> 23 24 enum RequestType { ZERO_BALANCE = 1, CREDIT_BALANCE, 25 DEBIT_BALANCE, END };26 int getRequest();27 bool shouldDisplay( int, double );28 void outputLine( int, const char * const, double );2930 int main()31 {32 // ifstream constructor opens the file

Page 14: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.1 Open file for input

1.2 Test for errors

1.3 Initialize variables

2. Prompt user

2.1 Get input

2.2 switch statement

33 ifstream inClientFile( "clients.dat", ios::in );3435 if ( !inClientFile ) {36 cerr << "File could not be opened" << endl;37 exit( 1 );38 }3940 int request, account;41 char name[ 30 ];42 double balance;4344 cout << "Enter request\n"45 << " 1 - List accounts with zero balances\n"46 << " 2 - List accounts with credit balances\n"47 << " 3 - List accounts with debit balances\n" 48 << " 4 - End of run"49 << setiosflags( ios::fixed | ios::showpoint );50 request = getRequest();5152 while ( request != END ) {5354 switch ( request ) {55 case ZERO_BALANCE:56 cout << "\nAccounts with zero balances:\n";57 break;58 case CREDIT_BALANCE:59 cout << "\nAccounts with credit balances:\n";60 break;61 case DEBIT_BALANCE:62 cout << "\nAccounts with debit balances:\n";63 break;64 }

Page 15: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

2.3 Input data from file

2.4 Decide whether to display data

2.5 Reset eof for next input

2.6 Move to beginning of file

2.7 Loop until END entered

3. Function definitions

6566 inClientFile >> account >> name >> balance;6768 while ( !inClientFile.eof() ) {69 if ( shouldDisplay( request, balance ) )70 outputLine( account, name, balance );7172 inClientFile >> account >> name >> balance;73 }74 75 inClientFile.clear(); // reset eof for next input76 inClientFile.seekg( 0 ); // move to beginning of file77 request = getRequest();78 }7980 cout << "End of run." << endl;8182 return 0; // ifstream destructor closes the file83 }8485 int getRequest()86 { 87 int request;88 89 do {90 cout << "\n? ";91 cin >> request;92 } while( request < ZERO_BALANCE && request > END );9394 return request;95 }9697 bool shouldDisplay( int type, double balance )

read data from file, copy into account, name and balance

Put file position pointer back to beginning.

Page 16: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

3. Function definitions

98 {

99 if ( type == CREDIT_BALANCE && balance < 0 )

100 return true;

101

102 if ( type == DEBIT_BALANCE && balance > 0 )

103 return true;

104

105 if ( type == ZERO_BALANCE && balance == 0 )

106 return true;

107

108 return false;

109}

110

111void outputLine( int acct, const char * const name, double bal )

112{

113 cout << setiosflags( ios::left ) << setw( 10 ) << acct

114 << setw( 13 ) << name << setw( 7 ) << setprecision( 2 )

115 << resetiosflags( ios::left )

116 << bal << '\n';

117}

Page 17: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

Enter request 1 - List accounts with zero balances 2 - List accounts with credit balances 3 - List accounts with debit balances 4 - End of run? 1 Accounts with zero balances:300 White 0.00? 2 Accounts with credit balances:400 Stone -42.16? 3 Accounts with debit balances:100 Jones 24.98200 Doe 345.67500 Rich 224.62 ? 4End of run.

Page 18: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.6 Updating Sequential Access Files

• Sequential access file – Cannot be modified without the risk of destroying other data

– Formatted text which is output to a file is very different than the internal representation

300 White 0.00 400 Jones 32.87 (old data in file)

if we want to change White's name to Worthington,

300 White 0.00 400 Jones 32.87 300 Worthington 0.00ones 32.87

300 Worthington 0.00

Data gets overwritten

Page 19: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.7 Random Access Files

• Random access files – access individual records without searching through other records

– Instant access to records in a file

– Data can be inserted without destroying other data

– Data previously stored can be updated or deleted without overwriting.

• Implemented using fixed length records

0 200 300 400 500

byte offsets}

} } } } } }

100

100bytes

100bytes

100bytes

100bytes

100bytes

100bytes

Page 20: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.8 Creating a Random Access File

• write - outputs a fixed number of bytes beginning at a specific location in memory to the specified stream– When writing an integer number to a file,

outFile.write( reinterpret_cast<const char *>( &number ), sizeof( number ) );

– First argument: pointer of type const char * (location to write from)

• address of number cast into a pointer

– Second argument: number of bytes to write(sizeof(number))• recall that data is represented internally in binary (thus, integers can

be stored in 4 bytes)

– Do not useoutFile << number;• could print 1 to 11 digits, each digit taking up a byte of storage

Page 21: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1. Define struct

1.1 Load headers

1.2 Initialize ofstream object

1 // Fig. 14.11: clntdata.h2 // Definition of struct clientData used in 3 // Figs. 14.11, 14.12, 14.14 and 14.15.4 #ifndef CLNTDATA_H5 #define CLNTDATA_H67 struct clientData {8 int accountNumber;9 char lastName[ 15 ];10 char firstName[ 10 ];11 double balance;12 };1314 #endif15 // Fig. 14.11: fig14_11.cpp16 // Creating a randomly accessed file sequentially17 #include <iostream>1819 using std::cerr;20 using std::endl;21 using std::ios;2223 #include <fstream>2425 using std::ofstream;2627 #include <cstdlib>2829 #include "clntdata.h"3031 int main()32 {33 ofstream outCredit( "credit.dat", ios::binary );34

Page 22: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.3 Check for file open

1.4 Initialize struct variable

2. Loop and write data to file

35 if ( !outCredit ) {

36 cerr << "File could not be opened." << endl;

37 exit( 1 );

38 }

39

40 clientData blankClient = { 0, "", "", 0.0 };

41

42 for ( int i = 0; i < 100; i++ )

43 outCredit.write(

44 reinterpret_cast<const char *>( &blankClient ),

45 sizeof( clientData ) );

46 return 0;

47 }Creates a random access file by writing equally-sized groups of data to the file. Each group is of size sizeof(clientData)

Page 23: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.9 Writing Data Randomly to a Random Access File

• seekp can be used in combination with write to store data at exact locations in an output file

Page 24: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1. Load header

1.1 Initialize ofstream object

1.2 Check for file open

1 // Fig. 14.12: fig14_12.cpp

2 // Writing to a random access file

3 #include <iostream>

4

5 using std::cerr;

6 using std::endl;

7 using std::cout;

8 using std::cin;

9 using std::ios;

10

11 #include <fstream>

12

13 using std::ofstream;

14

15 #include <cstdlib>

16 #include "clntdata.h"

17

18 int main()

19 {

20 ofstream outCredit( "credit.dat", ios::binary );

21

22 if ( !outCredit ) {

23 cerr << "File could not be opened." << endl;

24 exit( 1 );

25 }

Page 25: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

2. Input data

2.1 Set pointer to appropriate place in file

2.2 Write to file

2.3 Loop until end

26

27 cout << "Enter account number "

28 << "(1 to 100, 0 to end input)\n? ";

29

30 clientData client;

31 cin >> client.accountNumber;

32

33 while ( client.accountNumber > 0 &&

34 client.accountNumber <= 100 ) {

35 cout << "Enter lastname, firstname, balance\n? ";

36 cin >> client.lastName >> client.firstName

37 >> client.balance;

38

39 outCredit.seekp( ( client.accountNumber - 1 ) *

40 sizeof( clientData ) );

41 outCredit.write(

42 reinterpret_cast<const char *>( &client ),

43 sizeof( clientData ) );

44

45 cout << "Enter account number\n? ";

46 cin >> client.accountNumber;

47 }

48

49 return 0;

50 }

Enter account number (1 to 100, 0 to end input)

Enter lastname, firstname, balance

? Barker Doug 0.00

Find the appropriate position in the file. The groups are of size sizeof(clientData), and the first position is at location 0. Accounts sorted by accountNumber

Page 26: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

Enter account number (1 to 100, 0 to end input)? 37Enter lastname, firstname, balance? Barker Doug 0.00Enter account number? 29Enter lastname, firstname, balance? Brown Nancy -24.54Enter account number? 96Enter lastname, firstname, balance? Stone Sam 34.98Enter account number? 88Enter lastname, firstname, balance? Smith Dave 258.34Enter account number? 33Enter lastname, firstname, balance? Dunn Stacey 314.33Enter account number? 0

Page 27: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.10 Reading Data Sequentially from a Random Access File

• read – - inputs a fixed number of bytes from the specified stream to an area

in memory beginning at a specified address

– similar to write

inFile.read( reinterpret_cast<char *>( &number ), sizeof( int ) );

– First argument: pointer of type char * (location to put bytes)

– Second argument: number of bytes to read: sizeof( int )– Do not use

inFile >> number;

• fast sorting with direct access techniques

Page 28: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1. Load header

1.1 Initialize ifstream object

1.2 Check for file open

1 // Fig. 14.14: fig14_14.cpp2 // Reading a random access file sequentially3 #include <iostream>45 using std::cout;6 using std::endl;7 using std::ios;8 using std::cerr;910 #include <iomanip>1112 using std::setprecision;13 using std::setiosflags;14 using std::resetiosflags;15 using std::setw;1617 #include <fstream>1819 using std::ifstream;20 using std::ostream;2122 #include <cstdlib>23 #include "clntdata.h"24 25 void outputLine( ostream&, const clientData & );2627 int main()28 {29 ifstream inCredit( "credit.dat", ios::in );3031 if ( !inCredit ) {32 cerr << "File could not be opened." << endl;33 exit( 1 );

Page 29: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.3 Initialize struct object

2. Read in data and put it into client

3. Function definition

34 }3536 cout << setiosflags( ios::left ) << setw( 10 ) << "Account"37 << setw( 16 ) << "Last Name" << setw( 11 )38 << "First Name" << resetiosflags( ios::left )39 << setw( 10 ) << "Balance" << endl;4041 clientData client;4243 inCredit.read( reinterpret_cast<char *>( &client ), 44 sizeof( clientData ) );4546 while ( inCredit && !inCredit.eof() ) {4748 if ( client.accountNumber != 0 )49 outputLine( cout, client );5051 inCredit.read( reinterpret_cast<char *>( &client ),52 sizeof( clientData ) );53 }5455 return 0;56 }5758 void outputLine( ostream &output, const clientData &c )59 {60 output << setiosflags( ios::left ) << setw( 10 ) 61 << c.accountNumber << setw( 16 ) << c.lastName 62 << setw( 11 ) << c.firstName << setw( 10 ) 63 << setprecision( 2 ) << resetiosflags( ios::left ) 64 << setiosflags( ios::fixed | ios::showpoint )65 << c.balance << '\n';66 }

Inputs sizeof( clientData) bytes and puts it into &client (the memory address of client).

Outputs data in client to the screen.

Uses eof function to test for the end of file.

Page 30: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

Account Last Name First Name Balance29 Brown Nancy -24.5433 Dunn Stacey 314.3337 Barker Doug 0.0088 Smith Dave 258.3496 Stone Sam 34.98

Page 31: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.11 Example: A Transaction Processing Program

• The following example uses random access files to achieve instant access processing of a bank’s account information.

• We will– update existing accounts

– add new accounts

– delete accounts

– store a formatted listing of all accounts in a text file

Page 32: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1. Load headers

1.1 Function prototypes

1 // Fig. 14.15: fig14_15.cpp2 // This program reads a random access file sequentially,3 // updates data already written to the file, creates new4 // data to be placed in the file, and deletes data5 // already in the file.6 #include <iostream>78 using std::cout;9 using std::cerr;10 using std::cin;11 using std::endl;12 using std::ios;1314 #include <fstream>1516 using std::ofstream;17 using std::ostream;18 using std::fstream;1920 #include <iomanip>2122 using std::setiosflags;23 using std::resetiosflags;24 using std::setw;25 using std::setprecision;2627 #include <cstdlib>28 #include "clntdata.h"2930 int enterChoice();31 void textFile( fstream& );32 void updateRecord( fstream& );33 void newRecord( fstream& );

Page 33: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.2 enumeration

1.3 Initialize fstream object

1.4 Test for file open

1.5 Initialize variable

2. while loop and switch statements

34 void deleteRecord( fstream& );35 void outputLine( ostream&, const clientData & );36 int getAccount( const char * const );3738 enum Choices { TEXTFILE = 1, UPDATE, NEW, DELETE, END };3940 int main()41 {42 fstream inOutCredit( "credit.dat", ios::in | ios::out );4344 if ( !inOutCredit ) {45 cerr << "File could not be opened." << endl;46 exit ( 1 );47 }48 49 int choice;5051 while ( ( choice = enterChoice() ) != END ) {5253 switch ( choice ) {54 case TEXTFILE:55 textFile( inOutCredit );56 break;57 case UPDATE:58 updateRecord( inOutCredit );59 break;60 case NEW:61 newRecord( inOutCredit );62 break;63 case DELETE:64 deleteRecord( inOutCredit );65 break;66 default:

fstream objects can both input and output.

Calls function enterChoice

Page 34: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

3. Function definitions

67 cerr << "Incorrect choice\n";68 break;69 }7071 inOutCredit.clear(); // resets end-of-file indicator72 }7374 return 0;75 }7677 // Prompt for and input menu choice78 int enterChoice()79 {80 cout << "\nEnter your choice" << endl81 << "1 - store a formatted text file of accounts\n"82 << " called \"print.txt\" for printing\n"83 << "2 - update an account\n"84 << "3 - add a new account\n"85 << "4 - delete an account\n"86 << "5 - end program\n? ";8788 int menuChoice;89 cin >> menuChoice;90 return menuChoice;91 }9293 // Create formatted text file for printing94 void textFile( fstream &readFromFile )95 {96 ofstream outPrintFile( "print.txt", ios::out );9798 if ( !outPrintFile ) {99 cerr << "File could not be opened." << endl;100 exit( 1 );

Opens or creates print.txt

Page 35: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

3. Function definitions

101 }102103 outPrintFile << setiosflags( ios::left ) << setw( 10 ) 104 << "Account" << setw( 16 ) << "Last Name" << setw( 11 )105 << "First Name" << resetiosflags( ios::left ) 106 << setw( 10 ) << "Balance" << endl;107 readFromFile.seekg( 0 );108109 clientData client;110 readFromFile.read( reinterpret_cast<char *>( &client ),111 sizeof( clientData ) );112113 while ( !readFromFile.eof() ) {114 if ( client.accountNumber != 0 )115 outputLine( outPrintFile, client );116117 readFromFile.read( reinterpret_cast<char *>( &client ), 118 sizeof( clientData ) );119 }120}121122// Update an account's balance123void updateRecord( fstream &updateFile )124{125 int account = getAccount( "Enter account to update" );126127 updateFile.seekg( ( account - 1 ) * sizeof( clientData ) );128129 clientData client;130 updateFile.read( reinterpret_cast<char *>( &client ), 131 sizeof( clientData ) );132133 if ( client.accountNumber != 0 ) {

Outputs formatted data to print.txt

Moves to proper location in file based upon account number.

Input data to client

Page 36: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

3. Function definitions

134 outputLine( cout, client );135 cout << "\nEnter charge (+) or payment (-): ";136137 double transaction; // charge or payment138 cin >> transaction; // should validate139 client.balance += transaction;140 outputLine( cout, client );141 updateFile.seekp( ( account-1 ) * sizeof( clientData ) );142 updateFile.write( 143 reinterpret_cast<const char *>( &client ), 144 sizeof( clientData ) );145 }146 else147 cerr << "Account #" << account 148 << " has no information." << endl; 149}150151// Create and insert new record152void newRecord( fstream &insertInFile )153{154 int account = getAccount( "Enter new account number" );155156 insertInFile.seekg( ( account-1 ) * sizeof( clientData ) );157158 clientData client;159 insertInFile.read( reinterpret_cast<char *>( &client ), 160 sizeof( clientData ) );161162 if ( client.accountNumber == 0 ) {163 cout << "Enter lastname, firstname, balance\n? ";164 cin >> client.lastName >> client.firstName 165 >> client.balance;166 client.accountNumber = account;167 insertInFile.seekp( ( account - 1 ) *

Modify account balance, find proper location in file, and write to file.

Create a new record.

Check if account exists.

Input data.

Position pointer

Page 37: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

3. Function definitions

168 sizeof( clientData ) );

169 insertInFile.write(

170 reinterpret_cast<const char *>( &client ),

171 sizeof( clientData ) );

172 }

173 else

174 cerr << "Account #" << account

175 << " already contains information." << endl;

176}

177

178// Delete an existing record

179void deleteRecord( fstream &deleteFromFile )

180{

181 int account = getAccount( "Enter account to delete" );

182

183 deleteFromFile.seekg( (account-1) * sizeof( clientData ) );

184

185 clientData client;

186 deleteFromFile.read( reinterpret_cast<char *>( &client ),

187 sizeof( clientData ) );

188

189 if ( client.accountNumber != 0 ) {

190 clientData blankClient = { 0, "", "", 0.0 };

191

192 deleteFromFile.seekp( ( account - 1) *

193 sizeof( clientData ) );

194 deleteFromFile.write(

195 reinterpret_cast<const char *>( &blankClient ),

196 sizeof( clientData ) );

197 cout << "Account #" << account << " deleted." << endl;

198 }

Write new data to file

Delete record by replacing it with an empty account.

Page 38: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

3. Function definitions

199 else

200 cerr << "Account #" << account << " is empty." << endl;

201}

202

203// Output a line of client information

204void outputLine( ostream &output, const clientData &c )

205{

206 output << setiosflags( ios::left ) << setw( 10 )

207 << c.accountNumber << setw( 16 ) << c.lastName

208 << setw( 11 ) << c.firstName << setw( 10 )

209 << setprecision( 2 ) << resetiosflags( ios::left )

210 << setiosflags( ios::fixed | ios::showpoint )

211 << c.balance << '\n';

212}

213

214// Get an account number from the keyboard

215int getAccount( const char * const prompt )

216{

217 int account;

218

219 do {

220 cout << prompt << " (1 - 100): ";

221 cin >> account;

222 } while ( account < 1 || account > 100 );

223

224 return account;

225}

Formatted output.

Page 39: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

AFTER OPTION 1 PRINT.TXT CONTAINS:

Account Last Name First Name Balance29 Brown Nancy -24.5433 Dunn Stacey 314.3337 Barker Doug 0.0088 Smith Dave 258.3496 Stone Sam 34.98

Enter account to update (1 - 100): 3737 Barker Doug 0.00 Enter charge (+) or payment (-): +87.9937 Barker Doug 87.99

Enter new account number (1 - 100): 22Enter lastname, firstname, balance? Johnston Sarah 247.45

Enter account to delete (1 - 100): 29Account #29 deleted.

Page 40: 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

14.12 Input/Output of Objects

• When object data members output to a disk file– lose the object’s type information

– only have data bytes

• Possible solution– precede object output by specifying its type