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

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction and File Processing Lecture 13.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - File Processing Outline 14.1 Introduction 14.2 The Data Hierarchy 14.3 Files and Streams.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
CPSC 231 D.H. C++ File Processing 1 Learning Objectives §C++ I/O streams. §Reading and writing sequential files. §Reading and writing random access files.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2006 Pearson Education, Inc. All rights reserved File Processing.
計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
Lec11: File Processing 廖雪花 TEL: 年 5 月.
 2006 Pearson Education, Inc. All rights reserved File Processing.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
Lecture 11 – Files Operation. Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard.
1 Structure part 1 & File Processing. 2 Structures.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Programming Practice Introduction Tree Operations. Binary Search Tree. File Processing Create, read, write and update files. Sequential.
1 File Processing Dr. Serdar ÇELEBİ. 2 Outline Introduction The Data Hierarchy Files and Streams Creating a Sequential Access File Reading Data from a.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005.
 2008 Pearson Education, Inc. All rights reserved File Processing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - File Processing Outline 14.1 Introduction 14.2 The Data Hierarchy 14.3 Files and Streams.
Outline 1 Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü.
計算機程式語言 Lecture 17-1 國立台灣大學生物機電系 林達德 17 File Processing.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
 2000 Prentice Hall, Inc. All rights reserved Introduction Data files –Can be created, updated, and processed by C programs –Are used for permanent.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides:
File Processing Part 2. Random Access File In sequential access file, record in a file created with the formatted output function fprintf are not necessarily.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Exception Handling, File Processing Lecture 11 March 30, 2004.
Learners Support Publications Working with Files.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
1 Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating a Sequential Access File 11.5Reading.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management(cont.) Binary and random files.
2008YeungNam Univ. SE Lab. 1 C++ views each file as a sequence of bytes terminated by EOF-marker. Header files Files are opened by creating objects of.
Chapter 11 – File Processing
File Processing.
File Processing.
11 C File Processing.
File Processing Part 2.
17 File Processing.
TMF1414 Introduction to Programming
File Processing (Cont.) and Structures
CPSC 231 D.H. C++ File Processing
17 File Processing.
Chapter 14 – File Processing
Chapter 11 – File Processing
17 File Processing.
Topics Input and Output Streams More Detailed Error Testing
Fundamental of Programming (C)
Pertemuan 14 File Processing II
EPSII 59:006 Spring 2004.
C++ Programming Lecture 8 File Processing
EPSII 59:006 Spring 2004.
Chapter 11 Files chap8.
Presentation transcript:

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

 2000 Deitel & Associates, Inc. All rights reserved 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

 2000 Deitel & Associates, Inc. All rights reserved 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

 2000 Deitel & Associates, Inc. All rights reserved 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 Judy Green SallyBlack TomBlue JudyGreen IrisOrange Randy Red File Record Field Byte (ASCII character J) Bit

 2000 Deitel & Associates, Inc. All rights reserved 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 and –class ifstream - input –class ofstream - output –class fstream - either input or output

 2000 Deitel & Associates, Inc. All rights reserved 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: Makes a "line of communication" with the object and the file.

 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 4 5using std::cout; 6using std::cin; 7using std::ios; 8using std::cerr; 9using std::endl; 10 11#include 12 13using std::ofstream; 14 15#include 16 17int 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.

 2000 Deitel & Associates, Inc. All rights reserved. Outline 2. Test for file open 2.1 Input values 3. Output to file Program Output if ( !outClientFile ) { // overloaded ! operator 23 cerr << "File could not be opened" << endl; 24 exit( 1 ); // prototype in cstdlib 25 } cout << "Enter the account, name, and balance.\n" 28 << "Enter end-of-file to end input.\n? "; int account; 31 char name[ 30 ]; 32 double balance; while ( cin >> account >> name >> balance ) { 35 outClientFile << account << ' ' << name 36 << ' ' << balance << '\n'; 37 cout << "? "; 38 } return 0; // ofstream destructor closes file 41} Enter the account, name, and balance. Enter EOF to end input. ? 100 Jones ? 200 Doe ? 300 White 0.00 ? 400 Stone ? 500 Rich ? ^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

 2000 Deitel & Associates, Inc. All rights reserved 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 fileObject fileObject.seekg( n, ios::cur ); // position y bytes back from end of fileObject fileObject.seekg( y, ios::end ); // position at end of fileObject fileObject.seekg( 0, ios::end ); –tellg and tellp – return current location of pointer location = fileObject.tellg() //returns long

 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 4 5using std::cout; 6using std::cin; 7using std::ios; 8using std::cerr; 9using std::endl; 10 11#include 12 13using std::ifstream; 14 15#include 16 17using std::setiosflags; 18using std::resetiosflags; 19using std::setw; 20using std::setprecision; 21 22#include 23 24void outputLine( int, const char * const, double ); 25 26int main() 27{ 28 // ifstream constructor opens the file

 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 ); if ( !inClientFile ) { 32 cerr << "File could not be opened\n"; 33 exit( 1 ); 34 } int account; 37 char name[ 30 ]; 38 double balance; cout << setiosflags( ios::left ) << setw( 10 ) << "Account" 41 << setw( 13 ) << "Name" << "Balance\n" 42 << setiosflags( ios::fixed | ios::showpoint ); while ( inClientFile >> account >> name >> balance ) 45 outputLine( account, name, balance ); return 0; // ifstream destructor closes the file 48} 49 50void 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

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output Account Name Balance 100 Jones Doe White Stone Rich

 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.cpp 2// Credit inquiry program 3#include 4 5using std::cout; 6using std::cin; 7using std::ios; 8using std::cerr; 9using std::endl; 10 11#include 12 13using std::ifstream; 14 15#include 16 17using std::setiosflags; 18using std::resetiosflags; 19using std::setw; 20using std::setprecision; 21 22#include 23 24enum RequestType { ZERO_BALANCE = 1, CREDIT_BALANCE, 25 DEBIT_BALANCE, END }; 26int getRequest(); 27bool shouldDisplay( int, double ); 28void outputLine( int, const char * const, double ); 29 30int main() 31{ 32 // ifstream constructor opens the file

 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 ); if ( !inClientFile ) { 36 cerr << "File could not be opened" << endl; 37 exit( 1 ); 38 } int request, account; 41 char name[ 30 ]; 42 double balance; 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(); while ( request != END ) { 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 }

 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 inClientFile >> account >> name >> balance; while ( !inClientFile.eof() ) { 69 if ( shouldDisplay( request, balance ) ) 70 outputLine( account, name, balance ); inClientFile >> account >> name >> balance; 73 } inClientFile.clear(); // reset eof for next input 76 inClientFile.seekg( 0 ); // move to beginning of file 77 request = getRequest(); 78 } cout << "End of run." << endl; return 0; // ifstream destructor closes the file 83} 84 85int getRequest() 86{ 87 int request; do { 90 cout << "\n? "; 91 cin >> request; 92 } while( request END ); return request; 95} 96 97bool shouldDisplay( int type, double balance ) read data from file, copy into account, name and balance Put file position pointer back to beginning.

 2000 Deitel & Associates, Inc. All rights reserved. Outline 3. Function definitions 98{ 99 if ( type == CREDIT_BALANCE && balance < 0 ) 100 return true; if ( type == DEBIT_BALANCE && balance > 0 ) 103 return true; if ( type == ZERO_BALANCE && balance == 0 ) 106 return true; return false; 109} void 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}

 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 ? 3 Accounts with debit balances: 100 Jones Doe Rich ? 4 End of run.

 2000 Deitel & Associates, Inc. All rights reserved 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 Jones (old data in file) if we want to change White's name to Worthington, 300 White Jones Worthington 0.00ones Worthington 0.00 Data gets overwritten

 2000 Deitel & Associates, Inc. All rights reserved 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

 2000 Deitel & Associates, Inc. All rights reserved 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 ( &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 use outFile << number; could print 1 to 11 digits, each digit taking up a byte of storage

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Define struct 1.1 Load headers 1.2 Initialize ofstream object 1// Fig : clntdata.h 2// Definition of struct clientData used in 3// Figs , 14.12, and #ifndef CLNTDATA_H 5#define CLNTDATA_H 6 7struct clientData { 8 int accountNumber; 9 char lastName[ 15 ]; 10 char firstName[ 10 ]; 11 double balance; 12}; 13 14#endif 15// Fig : fig14_11.cpp 16// Creating a randomly accessed file sequentially 17#include 18 19using std::cerr; 20using std::endl; 21using std::ios; 22 23#include 24 25using std::ofstream; 26 27#include 28 29#include "clntdata.h" 30 31int main() 32{ 33 ofstream outCredit( "credit.dat", ios::binary ); 34

 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 } clientData blankClient = { 0, "", "", 0.0 }; for ( int i = 0; i < 100; i++ ) 43 outCredit.write( 44 reinterpret_cast ( &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)

 2000 Deitel & Associates, Inc. All rights reserved 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

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Load header 1.1 Initialize ofstream object 1.2 Check for file open 1// Fig : fig14_12.cpp 2// Writing to a random access file 3#include 4 5using std::cerr; 6using std::endl; 7using std::cout; 8using std::cin; 9using std::ios; 10 11#include 12 13using std::ofstream; 14 15#include 16#include "clntdata.h" 17 18int main() 19{ 20 ofstream outCredit( "credit.dat", ios::binary ); if ( !outCredit ) { 23 cerr << "File could not be opened." << endl; 24 exit( 1 ); 25 }

 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 cout << "Enter account number " 28 << "(1 to 100, 0 to end input)\n? "; clientData client; 31 cin >> client.accountNumber; while ( client.accountNumber > 0 && 34 client.accountNumber <= 100 ) { 35 cout << "Enter lastname, firstname, balance\n? "; 36 cin >> client.lastName >> client.firstName 37 >> client.balance; outCredit.seekp( ( client.accountNumber - 1 ) * 40 sizeof( clientData ) ); 41 outCredit.write( 42 reinterpret_cast ( &client ), 43 sizeof( clientData ) ); cout << "Enter account number\n? "; 46 cin >> client.accountNumber; 47 } 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

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output Enter account number (1 to 100, 0 to end input) ? 37 Enter lastname, firstname, balance ? Barker Doug 0.00 Enter account number ? 29 Enter lastname, firstname, balance ? Brown Nancy Enter account number ? 96 Enter lastname, firstname, balance ? Stone Sam Enter account number ? 88 Enter lastname, firstname, balance ? Smith Dave Enter account number ? 33 Enter lastname, firstname, balance ? Dunn Stacey Enter account number ? 0

 2000 Deitel & Associates, Inc. All rights reserved 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 ( &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

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Load header 1.1 Initialize ifstream object 1.2 Check for file open 1// Fig : fig14_14.cpp 2// Reading a random access file sequentially 3#include 4 5using std::cout; 6using std::endl; 7using std::ios; 8using std::cerr; 9 10#include 11 12using std::setprecision; 13using std::setiosflags; 14using std::resetiosflags; 15using std::setw; 16 17#include 18 19using std::ifstream; 20using std::ostream; 21 22#include 23#include "clntdata.h" 24 25void outputLine( ostream&, const clientData & ); 26 27int main() 28{ 29 ifstream inCredit( "credit.dat", ios::in ); if ( !inCredit ) { 32 cerr << "File could not be opened." << endl; 33 exit( 1 );

 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 } 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; clientData client; inCredit.read( reinterpret_cast ( &client ), 44 sizeof( clientData ) ); while ( inCredit && !inCredit.eof() ) { if ( client.accountNumber != 0 ) 49 outputLine( cout, client ); inCredit.read( reinterpret_cast ( &client ), 52 sizeof( clientData ) ); 53 } return 0; 56} 57 58void 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.

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output Account Last Name First Name Balance 29 Brown Nancy Dunn Stacey Barker Doug Smith Dave Stone Sam 34.98

 2000 Deitel & Associates, Inc. All rights reserved 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

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Load headers 1.1 Function prototypes 1// Fig : fig14_15.cpp 2// This program reads a random access file sequentially, 3// updates data already written to the file, creates new 4// data to be placed in the file, and deletes data 5// already in the file. 6#include 7 8using std::cout; 9using std::cerr; 10using std::cin; 11using std::endl; 12using std::ios; 13 14#include 15 16using std::ofstream; 17using std::ostream; 18using std::fstream; 19 20#include 21 22using std::setiosflags; 23using std::resetiosflags; 24using std::setw; 25using std::setprecision; 26 27#include 28#include "clntdata.h" 29 30int enterChoice(); 31void textFile( fstream& ); 32void updateRecord( fstream& ); 33void newRecord( fstream& );

 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 34void deleteRecord( fstream& ); 35void outputLine( ostream&, const clientData & ); 36int getAccount( const char * const ); 37 38enum Choices { TEXTFILE = 1, UPDATE, NEW, DELETE, END }; 39 40int main() 41{ 42 fstream inOutCredit( "credit.dat", ios::in | ios::out ); if ( !inOutCredit ) { 45 cerr << "File could not be opened." << endl; 46 exit ( 1 ); 47 } int choice; while ( ( choice = enterChoice() ) != END ) { 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

 2000 Deitel & Associates, Inc. All rights reserved. Outline 3. Function definitions 67 cerr << "Incorrect choice\n"; 68 break; 69 } inOutCredit.clear(); // resets end-of-file indicator 72 } return 0; 75} 76 77// Prompt for and input menu choice 78int enterChoice() 79{ 80 cout << "\nEnter your choice" << endl 81 << "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? "; int menuChoice; 89 cin >> menuChoice; 90 return menuChoice; 91} 92 93// Create formatted text file for printing 94void textFile( fstream &readFromFile ) 95{ 96 ofstream outPrintFile( "print.txt", ios::out ); if ( !outPrintFile ) { 99 cerr << "File could not be opened." << endl; 100 exit( 1 ); Opens or creates print.txt

 2000 Deitel & Associates, Inc. All rights reserved. Outline 3. Function definitions 101 } 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 ); clientData client; 110 readFromFile.read( reinterpret_cast ( &client ), 111 sizeof( clientData ) ); while ( !readFromFile.eof() ) { 114 if ( client.accountNumber != 0 ) 115 outputLine( outPrintFile, client ); readFromFile.read( reinterpret_cast ( &client ), 118 sizeof( clientData ) ); 119 } 120} // Update an account's balance 123void updateRecord( fstream &updateFile ) 124{ 125 int account = getAccount( "Enter account to update" ); updateFile.seekg( ( account - 1 ) * sizeof( clientData ) ); clientData client; 130 updateFile.read( reinterpret_cast ( &client ), 131 sizeof( clientData ) ); if ( client.accountNumber != 0 ) { Outputs formatted data to print.txt Moves to proper location in file based upon account number. Input data to client

 2000 Deitel & Associates, Inc. All rights reserved. Outline 3. Function definitions 134 outputLine( cout, client ); 135 cout << "\nEnter charge (+) or payment (-): "; double transaction; // charge or payment 138 cin >> transaction; // should validate 139 client.balance += transaction; 140 outputLine( cout, client ); 141 updateFile.seekp( ( account-1 ) * sizeof( clientData ) ); 142 updateFile.write( 143 reinterpret_cast ( &client ), 144 sizeof( clientData ) ); 145 } 146 else 147 cerr << "Account #" << account 148 << " has no information." << endl; 149} // Create and insert new record 152void newRecord( fstream &insertInFile ) 153{ 154 int account = getAccount( "Enter new account number" ); insertInFile.seekg( ( account-1 ) * sizeof( clientData ) ); clientData client; 159 insertInFile.read( reinterpret_cast ( &client ), 160 sizeof( clientData ) ); 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

 2000 Deitel & Associates, Inc. All rights reserved. Outline 3. Function definitions 168 sizeof( clientData ) ); 169 insertInFile.write( 170 reinterpret_cast ( &client ), 171 sizeof( clientData ) ); 172 } 173 else 174 cerr << "Account #" << account 175 << " already contains information." << endl; 176} // Delete an existing record 179void deleteRecord( fstream &deleteFromFile ) 180{ 181 int account = getAccount( "Enter account to delete" ); deleteFromFile.seekg( (account-1) * sizeof( clientData ) ); clientData client; 186 deleteFromFile.read( reinterpret_cast ( &client ), 187 sizeof( clientData ) ); if ( client.accountNumber != 0 ) { 190 clientData blankClient = { 0, "", "", 0.0 }; deleteFromFile.seekp( ( account - 1) * 193 sizeof( clientData ) ); 194 deleteFromFile.write( 195 reinterpret_cast ( &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.

 2000 Deitel & Associates, Inc. All rights reserved. Outline 3. Function definitions 199 else 200 cerr << "Account #" << account << " is empty." << endl; 201} // 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} // Get an account number from the keyboard 215int getAccount( const char * const prompt ) 216{ 217 int account; do { 220 cout << prompt << " ( ): "; 221 cin >> account; 222 } while ( account 100 ); return account; 225} Formatted output.

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output AFTER OPTION 1 PRINT.TXT CONTAINS: Account Last Name First Name Balance 29 Brown Nancy Dunn Stacey Barker Doug Smith Dave Stone Sam Enter account to update ( ): Barker Doug 0.00 Enter charge (+) or payment (-): Barker Doug Enter new account number ( ): 22 Enter lastname, firstname, balance ? Johnston Sarah Enter account to delete ( ): 29 Account #29 deleted.

 2000 Deitel & Associates, Inc. All rights reserved 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