file processing

31
File Processing File Processing Outline Introduction The Data Hierarchy Files and Streams Creating a Sequential-Access File Reading Data from a Sequential-Access File Updating Sequential-Access Files Random-Access Files Creating a Random-Access File Writing Data Randomly to a Random- Access File Reading Data Sequentially from a Random- Access File Example: A Transaction-Processing Program Input/Output of Objects

Upload: engr-syed-ziafat-ali

Post on 18-Nov-2014

186 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: File Processing

File ProcessingFile ProcessingOutline

IntroductionThe Data HierarchyFiles and StreamsCreating a Sequential-Access FileReading Data from a Sequential-Access FileUpdating Sequential-Access FilesRandom-Access FilesCreating a Random-Access File

Writing Data Randomly to a Random-Access File Reading Data Sequentially from a Random-Access

File Example: A Transaction-Processing Program

Input/Output of Objects

Page 2: File Processing

IntroductionIntroduction

Storage of dataStorage of data Arrays, variables are temporaryArrays, variables are temporary Files are permanentFiles are permanent Magnetic disk, optical disk, tapesMagnetic disk, optical disk, tapes

In this PresentationIn this Presentation Create, update, process filesCreate, update, process files Sequential and random accessSequential and random access Formatted and raw processingFormatted and raw processing

Page 3: File Processing

Some Definitions (Data Hierarchy)Some Definitions (Data Hierarchy)

Field: group of characters with some meaningField: group of characters with some meaning Your nameYour name

Record: group of related fieldsRecord: group of related fields structstruct or or classclass in C++ in C++ In payroll system, could be name, SS#, address, wageIn payroll system, could be name, SS#, address, wage Each field associated with same employeeEach field associated with same employee Record key: field used to uniquely identify recordRecord key: field used to uniquely identify record

File: group of related recordsFile: group of related records Payroll for entire companyPayroll for entire company Sequential file: records stored by keySequential file: records stored by key

Database: group of related filesDatabase: group of related files Payroll, accounts-receivable, inventory…Payroll, accounts-receivable, inventory…

Page 4: File Processing

The Data HierarchyThe Data Hierarchy

1

01001010

Judy

Judy Green

Sally BlackTom BlueJudy GreenIris OrangeRandyRed

File

Record

Field

Byte (ASCII character J)

Bit

Page 5: File Processing

Files and StreamsFiles and Streams

C++ views file as sequence of bytesC++ views file as sequence of bytes Ends with Ends with end-of-fileend-of-file marker marker

When a File is open 3 streams are associated or When a File is open 3 streams are associated or opened with itopened with it

1. Standard Input1. Standard Input

2. Standard Output2. Standard Output

3. Standard Error3. Standard Error

0 31 2 4 5 8 9

...

... n-1

end-of-file marker

6 7

Page 6: File Processing

Creating a Sequential-Access Creating a Sequential-Access FileFile

C++ imposes no structure on fileC++ imposes no structure on file Concept of "record" must be implemented by programmerConcept of "record" must be implemented by programmer

To open file, create objectsTo open file, create objects Creates "line of communication" from object to fileCreates "line of communication" from object to file ClassesClasses

ifstreamifstream (input only) (input only) ofstreamofstream (output only) (output only) fstreamfstream (I/O) (I/O)

Constructors take Constructors take file namefile name and and file-open modefile-open modeofstream outClientFile( "filename", ofstream outClientFile( "filename", fileOpenModefileOpenMode ); );

To attach a file laterTo attach a file laterOfstream outClientFile;Ofstream outClientFile;outClientFile.open( "filename", outClientFile.open( "filename", fileOpenModefileOpenMode););

Page 7: File Processing

Creating a Sequential-Access FileCreating a Sequential-Access File

File-open modesFile-open modesMode 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.

Page 8: File Processing

Creating a Sequential-Access FileCreating a Sequential-Access File

OperationsOperations Overloaded Overloaded operator!operator!

!outClientFile!outClientFile Returns nonzero (true) if Returns nonzero (true) if badbitbadbit or or failbitfailbit set set

Opened non-existent file for reading, wrong permissionsOpened non-existent file for reading, wrong permissions

Overloaded Overloaded operator void*operator void* Converts stream object to pointerConverts stream object to pointer 00 when when when when failbitfailbit or or badbitbadbit set, otherwise nonzero set, otherwise nonzero

failbitfailbit set when EOF found set when EOF found while ( cin >> myVariable )while ( cin >> myVariable )

Implicitly converts Implicitly converts cincin to pointer to pointer Loops until EOFLoops until EOF

Page 9: File Processing

Creating a Sequential-Access Creating a Sequential-Access FileFile

OperationsOperations Writing to file (just like Writing to file (just like coutcout))

outClientFile << myVariableoutClientFile << myVariable

Closing fileClosing file outClientFile.close()outClientFile.close() Automatically closed when destructor calledAutomatically closed when destructor called

Page 10: File Processing

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> // exit prototype16 17 int main()18 {19 // ofstream constructor opens file 20 ofstream outClientFile( "clients.dat", ios::out );21 22 // exit program if unable to create file23 if ( !outClientFile ) { // overloaded ! operator24 cerr << "File could not be opened" << endl;25 exit( 1 );26 27 } // end if

Notice the the header files required for file I/O.

ofstream object created and used to open file "clients.dat". If the file does not exist, it is created.

! operator used to test if the file opened properly.

Page 11: File Processing

28 29 cout << "Enter the account, name, and balance." << endl30 << "Enter end-of-file to end input.\n? ";31 32 int account;33 char name[ 30 ];34 double balance;35 36 // read account, name and balance from cin, then place in file37 while ( cin >> account >> name >> balance ) {38 outClientFile << account << ' ' << name << ' ' << balance39 << endl; 40 cout << "? ";41 42 } // end while43 44 return 0; // ofstream destructor closes file45 46 } // end main

cin is implicitly converted to a pointer. When EOF is encountered, it returns 0 and the loop stops.

Write data to file like a regular stream.

File closed when destructor called for object. Can be explicitly closed with close().

Page 12: File Processing

Reading Data from a Sequential-Reading Data from a Sequential-Access FileAccess File

Reading filesReading files ifstream inClientFile("filename",ios::in );ifstream inClientFile("filename",ios::in );

OverloadedOverloaded ! ! !inClientFile!inClientFile tests if file was opened properly tests if file was opened properly

operator void*operator void* converts to pointer converts to pointer while (inClientFile >> myVariable)while (inClientFile >> myVariable) Stops when EOF found (gets value Stops when EOF found (gets value 00))

Page 13: File Processing

28 int main()29 {30 // ifstream constructor opens the file 31 ifstream inClientFile( "clients.dat", ios::in );32 33 // exit program if ifstream could not open file34 if ( !inClientFile ) {35 cerr << "File could not be opened" << endl;36 exit( 1 );37 38 } // end if39 40 int account;41 char name[ 30 ];42 double balance;43 44 cout << left << setw( 10 ) << "Account" << setw( 13 ) 45 << "Name" << "Balance" << endl << fixed << showpoint;46 47 // display each record in file48 while ( inClientFile >> account >> name >> balance )49 outputLine( account, name, balance );50 51 return 0; // ifstream destructor closes the file52 53 } // end main

Page 14: File Processing

55 // display single record from file56 void outputLine( int account, const char * const name, 57 double balance )58 {59 cout << left << setw( 10 ) << account << setw( 13 ) << name60 << setw( 7 ) << setprecision( 2 ) << right << balance61 << endl;

Page 15: File Processing

Reading Data from a Sequential-Reading Data from a Sequential-Access FileAccess File

Examples:Examples: fileObject.seekg(0)fileObject.seekg(0)

Goes to front of file (location Goes to front of file (location 00) because ) because ios::begios::beg is default is default fileObject.seekg(n)fileObject.seekg(n)

Goes to nth byte from beginningGoes to nth byte from beginning fileObject.seekg(n, ios::cur)fileObject.seekg(n, ios::cur)

Goes n bytes forwardGoes n bytes forward fileObject.seekg(y, ios::end)fileObject.seekg(y, ios::end)

Goes y bytes back from endGoes y bytes back from end fileObject.seekg(0, ios::cur)fileObject.seekg(0, ios::cur)

Goes to last byteGoes to last byte seekpseekp similar similar

Page 16: File Processing

Updating Sequential-Access FilesUpdating Sequential-Access Files

Updating sequential filesUpdating sequential files Risk overwriting other dataRisk overwriting other data Example: change name "White" to "Worthington"Example: change name "White" to "Worthington"

Old dataOld data

300 White 0.00 400 Jones 32.87300 White 0.00 400 Jones 32.87 Insert new dataInsert new data

300 Worthington 0.00

300 White 0.00 400 Jones 32.87

300 Worthington 0.00ones 32.87

Data gets overwritten

Page 17: File Processing

Random-Access FilesRandom-Access Files

Instant accessInstant access Want to locate record quicklyWant to locate record quickly

Airline reservations, ATMsAirline reservations, ATMs

Sequential files must search through each oneSequential files must search through each one

Random-access files are solutionRandom-access files are solution Instant accessInstant access Insert record without destroying other dataInsert record without destroying other data Update/delete items without changing other dataUpdate/delete items without changing other data

Page 18: File Processing

Random-Access FilesRandom-Access Files

C++ imposes no structure on filesC++ imposes no structure on files Programmer must create random-access filesProgrammer must create random-access files Simplest way: fixed-length recordsSimplest way: fixed-length records

Calculate position in file from record size and keyCalculate position in file from record size and key

0 200 300 400 500

byte offsets}

} } } } } }

100

100bytes

100bytes

100bytes

100bytes

100bytes

100bytes

Page 19: File Processing

Creating a Random-Access Creating a Random-Access FileFile

<<<< operator and operator and write()write() outFile << numberoutFile << number

Outputs Outputs numbernumber ( (intint) as a ) as a char *char * Variable number of bytesVariable number of bytes

outFile.write( outFile.write( const char *const char *, , size size );); Outputs raw bytesOutputs raw bytes Takes pointer to memory location, number of bytes Takes pointer to memory location, number of bytes

to writeto write Copies data directly from memory into fileCopies data directly from memory into file Does not convert to Does not convert to char *char *

Page 20: File Processing

Creating a Random-Access FileCreating a Random-Access File

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

sizeof( number ) );sizeof( number ) );

&number&number is an is an int *int * Convert to Convert to const char *const char * with with reinterpret_castreinterpret_cast

sizeof(number)sizeof(number) Size of Size of numbernumber (an (an intint) in bytes) in bytes

readread function similar (more later) function similar (more later)

Page 21: File Processing

Creating a Random-Access FileCreating a Random-Access File

Usually write entire Usually write entire structstruct or object to file or object to file Problem statementProblem statement

Credit processing programCredit processing program Store at most 100 fixed-length recordsStore at most 100 fixed-length records RecordRecord

Account number (key)Account number (key) First and last nameFirst and last name BalanceBalance

Account operationsAccount operations Update, create new, delete, list all accounts in a fileUpdate, create new, delete, list all accounts in a file

Page 22: File Processing

#include <iostream>7 8 using std::string;9 10 class ClientData {11 12 public:13 15 ClientData( int = 0, string = "", string = "", double = 0.0 );16 18 void setAccountNumber( int );19 int getAccountNumber() const;20 22 void setLastName( string );23 string getLastName() const;26 void setFirstName( string );27 string getFirstName() const;28 30 void setBalance( double );31 double getBalance() const;32 33 private:34 int accountNumber;35 char lastName[ 15 ];36 char firstName[ 10 ];37 double balance;38 };

Page 23: File Processing

3 #include <iostream> 9 #include <fstream> 13 #include <cstdlib>14 #include "clientData.h" // ClientData class definition15 16 int main()17 {18 ofstream outCredit( "credit.dat", ios::binary );19 20 // exit program if ofstream could not open file21 if ( !outCredit ) {22 cerr << "File could not be opened." << endl;23 exit( 1 );24 }27 // create ClientData with no information28 ClientData blankClient;29 30 // output 100 blank records to file31 for ( int i = 0; i < 100; i++ )32 outCredit.write( 33 reinterpret_cast< const char * >( &blankClient ),34 sizeof( ClientData ) ); 35 36 return 0;37 38 } // end main

Page 24: File Processing

Writing Data Randomly to a Writing Data Randomly to a Random-Access FileRandom-Access File

Use Use seekpseekp to write to exact location in file to write to exact location in file Where does the first record begin?Where does the first record begin?

Byte 0Byte 0

The second record?The second record? Byte 0 + sizeof(object)Byte 0 + sizeof(object)

Any record?Any record? (Recordnum - 1) * sizeof(object)(Recordnum - 1) * sizeof(object)

Page 25: File Processing

3 3 #include <iostream>#include <iostream> 11 11 #include <iomanip>#include <iomanip> 12 12 using std::setw;using std::setw; 14 14 #include <fstream>#include <fstream> 16 16 using std::ofstream;using std::ofstream; 18 18 #include <cstdlib>#include <cstdlib> 20 20 #include "clientData.h“#include "clientData.h“ 22 22 int main()int main() 23 23 {{ 24 24 int accountNumber; int accountNumber; 25 25 char lastName[ 15 ]; char lastName[ 15 ]; 26 26 char firstName[ 10 ]; char firstName[ 10 ]; 27 27 double balance; double balance; 28 28 29 29 ofstream outCredit( "credit.dat", ios::binary ); ofstream outCredit( "credit.dat", ios::binary ); 30 30 31 31 // exit program if ofstream cannot open file // exit program if ofstream cannot open file 32 32 if ( !outCredit ) { if ( !outCredit ) { 33 33 cerr << "File could not be opened." << endl; cerr << "File could not be opened." << endl; 34 34 exit( 1 ); exit( 1 ); 35 35 36 36 } // end if } // end if 37 37 38 38 cout << "Enter account number " cout << "Enter account number " 39 39 << "(1 to 100, 0 to end input)\n? "; << "(1 to 100, 0 to end input)\n? "; 40 40 41 41 // require user to specify account number // require user to specify account number 42 42 ClientData client; ClientData client; 43 43 cin >> accountNumber; cin >> accountNumber; 44 44 client.setAccountNumber( accountNumber ); client.setAccountNumber( accountNumber );

Page 26: File Processing

46 46 // user enters information, which is copied into file // user enters information, which is copied into file 47 47 while ( client.getAccountNumber() > 0 && while ( client.getAccountNumber() > 0 && 48 48 client.getAccountNumber() <= 100 ) { client.getAccountNumber() <= 100 ) { 49 49 50 50 // user enters last name, first name and balance // user enters last name, first name and balance 51 51 cout << "Enter lastname, firstname, balance\n? "; cout << "Enter lastname, firstname, balance\n? "; 52 52 cin >> setw( 15 ) >> lastName; cin >> setw( 15 ) >> lastName; 53 53 cin >> setw( 10 ) >> firstName; cin >> setw( 10 ) >> firstName; 54 54 cin >> balance; cin >> balance; 55 55 56 56 // set record lastName, firstName and balance values // set record lastName, firstName and balance values 57 57 client.setLastName( lastName ); client.setLastName( lastName ); 58 58 client.setFirstName( firstName ); client.setFirstName( firstName ); 59 59 client.setBalance( balance ); client.setBalance( balance ); 60 60 61 61 // seek position in file of user-specified record // seek position in file of user-specified record 62 62 outCredit.seekp( ( client.getAccountNumber() - 1 ) * outCredit.seekp( ( client.getAccountNumber() - 1 ) * 63 63 sizeof( ClientData ) ); sizeof( ClientData ) ); 64 64 65 65 // write user-specified information in file // write user-specified information in file 66 66 outCredit.write( outCredit.write( 67 67 reinterpret_cast< const char * >( &client ), reinterpret_cast< const char * >( &client ), 68 68 sizeof( ClientData ) ); sizeof( ClientData ) );

Position outCredit to the proper location in the file (based on the account number).

Write ClientData object to file at specified position.

Page 27: File Processing

70 70 // enable user to specify another account // enable user to specify another account numbernumber

71 71 cout << "Enter account number\n? "; cout << "Enter account number\n? "; 72 72 cin >> accountNumber; cin >> accountNumber; 73 73 client.setAccountNumber( accountNumber ); client.setAccountNumber( accountNumber ); 74 74 75 75 } // end while } // end while 76 76 77 77 return 0; return 0; 78 78 79 79 } // end main} // end main

Page 28: File Processing

Reading Data Sequentially from a Reading Data Sequentially from a Random-Access FileRandom-Access File

readread - similar to - similar to writewrite Reads raw bytes from file into memoryReads raw bytes from file into memory inFile.read( reinterpret_cast<char *>( &number ), inFile.read( reinterpret_cast<char *>( &number ),

sizeof( int ) );sizeof( int ) );

&number&number: location to store data: location to store data sizeof(int)sizeof(int): how many bytes to read: how many bytes to read

Do not use Do not use inFile >> numberinFile >> number with raw bytes with raw bytes

Upcoming programUpcoming program Output data from a random-access fileOutput data from a random-access file Go through each record sequentiallyGo through each record sequentially

If no data (accountNumber == 0) then skipIf no data (accountNumber == 0) then skip

Page 29: File Processing

3 3 #include <iostream>#include <iostream> 14 14 #include <iomanip>#include <iomanip> 19 19 #include <fstream>#include <fstream> 24 24 #include <cstdlib> // exit protoyype#include <cstdlib> // exit protoyype 25 25 #include "clientData.h“#include "clientData.h“ 27 27 void outputLine( ostream&, const ClientData & );void outputLine( ostream&, const ClientData & ); 28 28 29 29 int main()int main() 30 30 {{ 31 31 ifstream inCredit( "credit.dat", ios::in ); ifstream inCredit( "credit.dat", ios::in ); 32 32 33 33 // exit program if ifstream cannot open file // exit program if ifstream cannot open file 34 34 if ( !inCredit ) { if ( !inCredit ) { 35 35 cerr << "File could not be opened." << endl; cerr << "File could not be opened." << endl; 36 36 exit( 1 ); exit( 1 ); 37 37 38 38 } // end if } // end if 39 39 40 40 cout << left << setw( 10 ) << "Account" << setw( 16 ) cout << left << setw( 10 ) << "Account" << setw( 16 ) 41 41 << "Last Name" << setw( 11 ) << "First Name" << left << "Last Name" << setw( 11 ) << "First Name" << left 42 42 << setw( 10 ) << right << "Balance" << endl; << setw( 10 ) << right << "Balance" << endl; 43 43 44 44 ClientData client; // create record ClientData client; // create record 45 45 46 46 // read first record from file // read first record from file 47 47 inCredit.read( reinterpret_cast< char * >( &client ), inCredit.read( reinterpret_cast< char * >( &client ), 48 48 sizeof( ClientData ) ); sizeof( ClientData ) ); 49 49

Read sizeof(ClientData) bytes and put into object client. This may be an empty record.

Page 30: File Processing

50 50 // read all records from file // read all records from file 51 51 while ( inCredit && !inCredit.eof() ) { while ( inCredit && !inCredit.eof() ) { 52 52 53 53 // display record // display record 54 54 if ( client.getAccountNumber() != 0 ) if ( client.getAccountNumber() != 0 ) 55 55 outputLine( cout, client ); outputLine( cout, client ); 56 56 57 57 // read next from file // read next from file 58 58 inCredit.read( reinterpret_cast< char * >( &client ), inCredit.read( reinterpret_cast< char * >( &client ), 59 59 sizeof( ClientData ) ); sizeof( ClientData ) ); 60 60 61 61 } // end while } // end while 62 62 63 63 return 0; return 0; 64 64 65 65 } // end main} // end main 66 66 67 67 // display single record// display single record 68 68 void outputLine( ostream &output, const ClientData &record )void outputLine( ostream &output, const ClientData &record ) 69 69 {{ 70 70 output << left << setw( 10 ) << record.getAccountNumber() output << left << setw( 10 ) << record.getAccountNumber() 71 71 << setw( 16 ) << record.getLastName().data() << setw( 16 ) << record.getLastName().data() 72 72 << setw( 11 ) << record.getFirstName().data() << setw( 11 ) << record.getFirstName().data() 73 73 << setw( 10 ) << setprecision( 2 ) << right << fixed << setw( 10 ) << setprecision( 2 ) << right << fixed 74 74 << showpoint << record.getBalance() << endl; << showpoint << record.getBalance() << endl; 75 75 76 76 } // end outputLine} // end outputLine

Page 31: File Processing

To open file for reading and writingTo open file for reading and writing Use Use fstreamfstream objectobject "Or" file-open modes together"Or" file-open modes togetherfstream inOutCredit( "credit.dat", ios::in | fstream inOutCredit( "credit.dat", ios::in |

ios::out );ios::out );