Lecture 14 IO and File management File stream

  • Slides: 18
Download presentation
Lecture 14 I/O and File management File stream. C ++ File stream classes. File

Lecture 14 I/O and File management File stream. C ++ File stream classes. File management functions. File modes. Lecturer Ali Mohammed Kadhim ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ

Files and Streams • C++ views file as sequence of bytes • Ends with

Files and Streams • C++ views file as sequence of bytes • Ends with end-of-file marker 0 1 2 3 4 5 6 7 8 9 . . . n-1 end-of-file marker • When file opened • Object created, stream associated with it • cin, cout, etc. created when <iostream> included • Communication between program and file/device ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 2

Files and Streams • To perform file processing • Include <iostream> and <fstream> •

Files and Streams • To perform file processing • Include <iostream> and <fstream> • Class templates • basic_ifstream (input) • basic_ofstream (output) • basic_fstream (I/O) • typedefs for specializations that allow char I/O • ifstream (char input) • ofstream (char output) • fstream (char I/O) ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 3

Files and Streams • Opening files • Create objects from template • Derive from

Files and Streams • Opening files • Create objects from template • Derive from stream classes • Can use stream methods • put, get, peek, etc. basic_ios basic_istream basic_ifstream ﺍﻻﻟﻜﺘﺮﻭﻧﻲ basic_ostream basic_iostream basic_ofstream basic_fstream ﺍﻟﻤﺤﺎﺿﺮﺍﺕ ﻧﻈﺎﻡ 4

Creating a Sequential-Access File • C++ imposes no structure on file • Concept of

Creating a Sequential-Access File • C++ imposes no structure on file • Concept of "record" must be implemented by programmer • To open file, create objects • Creates "line of communication" from object to file • Classes • ifstream (input only) • ofstream (output only) • fstream (I/O) • Constructors take file name and file-open mode ofstream out. Client. File( "filename", file. Open. Mode ); • To attach a file later ofstream out. Client. File; out. Client. File. open( "filename", file. Open. Mode); ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 5

Creating a Sequential-Access File-open modes • ofstream opened for output by default • ofstream

Creating a Sequential-Access File-open modes • ofstream opened for output by default • ofstream out. Client. File( "clients. dat", ios: : out ); • ofstream out. Client. File( "clients. dat"); ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 6

Creating a Sequential-Access File • Operations • Overloaded operator! • !out. Client. File •

Creating a Sequential-Access File • Operations • Overloaded operator! • !out. Client. File • Returns nonzero (true) if badbit or failbit set • Opened non-existent file for reading, wrong permissions • Overloaded operator void* • Converts stream object to pointer • 0 when failbit or badbit set, otherwise nonzero • failbit set when EOF found • while ( cin >> my. Variable ) • Implicitly converts cin to pointer • Loops until EOF ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 7

Creating a Sequential-Access File • Operations • Writing to file (just like cout) •

Creating a Sequential-Access File • Operations • Writing to file (just like cout) • out. Client. File << my. Variable • Closing file • out. Client. File. close() • Automatically closed when destructor called ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 8

fig 14_1. cpp (1 of 2) 1 2 3 4 5 6 7 8

fig 14_1. cpp (1 of 2) 1 2 3 4 5 6 7 8 // Fig. 14. 1 cpp // Create a sequential file. #include <iostream> using std: : cout; using std: : cin; using std: : ios; using std: : cerr; using std: : endl; 9 10 11 #include <fstream> using std: : ofstream; 12 13 15 16 17 #include <cstdlib> // exit prototype int main() { // ofstream constructor opens file ofstream out. Client. File( "clients. dat", ios: : out ); 18 ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 9

19 20 21 22 23 25 26 28 29 30 31 32 33 34

19 20 21 22 23 25 26 28 29 30 31 32 33 34 35 36 37 38 // exit program if unable to create file if ( !out. Client. File ) { // overloaded ! operator cerr << "File could not be opened" << endl; exit( 1 ); } // end if cout << "Enter the account, name, and balance. " << endl << "Enter end-of-file to end input. n? "; int account; char name[ 30 ]; double balance; // read account, name and balance from cin, then place in file while ( cin >> account >> name >> balance ) { out. Client. File << account << ' ' << name << ' ' << balance << endl; cout << "? "; } // end while return 0; // ofstream destructor closes file } // end main ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ fig 14_1. cpp (2 of 2) 10

fig 14_1. cpp output : Enter ? 100 ? 200 ? 300 ? 400

fig 14_1. cpp output : Enter ? 100 ? 200 ? 300 ? 400 ? 500 ? ^Z the account, name, and balance. end-of-file to end input. Jones 24. 98 Doe 345. 67 White 0. 00 Stone -42. 16 Rich 224. 62 ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 11

Reading Data from a Sequential-Access File • Reading files • ifstream in. Client. File(

Reading Data from a Sequential-Access File • Reading files • ifstream in. Client. File( "filename", ios: : in ); • Overloaded ! • !in. Client. File tests if file was opened properly • operator void* converts to pointer • while (in. Client. File >> my. Variable) • Stops when EOF found (gets value 0) ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 12

Reading Data from a Sequential-Access File • File position pointers • Number of next

Reading Data from a Sequential-Access File • File position pointers • Number of next byte to read/write • Functions to reposition pointer • seekg (seek get for istream class) • seekp (seek put for ostream class) • Classes have "get" and "put" pointers • seekg and seekp take offset and direction • Offset: number of bytes relative to direction • Direction (ios: : beg default) • ios: : beg - relative to beginning of stream • ios: : cur - relative to current position • ios: : end - relative to end ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 13

Reading Data from a Sequential-Access File • Examples • file. Object. seekg(0) • Goes

Reading Data from a Sequential-Access File • Examples • file. Object. seekg(0) • Goes to front of file (location 0) because ios: : beg is default • file. Object. seekg(n) • Goes to nth byte from beginning • file. Object. seekg(n, ios: : cur) • Goes n bytes forward • file. Object. seekg(y, ios: : end) • Goes y bytes back from end • file. Object. seekg(0, ios: : cur) • Goes to last byte • seekp similar ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 14

Reading Data from a Sequential-Access File • To find pointer location • tellg and

Reading Data from a Sequential-Access File • To find pointer location • tellg and tellp • location = file. Object. tellg() • Upcoming example • Credit manager program • List accounts with zero balance, credit, and debit ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 15

Fig. 14. 2 52 53 // process user's request while ( request != END

Fig. 14. 2 52 53 // process user's request while ( request != END ) { 54 55 57 58 59 switch ( request ) { case ZERO_BALANCE: cout << "n. Accounts with zero balances: n"; break; 60 61 62 63 case CREDIT_BALANCE: cout << "n. Accounts with credit balances: n"; break; 64 65 66 67 case DEBIT_BALANCE: cout << "n. Accounts with debit balances: n"; break; 68 69 } // end switch 70 ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 16

70 // read account, name and balance from file 71 in. Client. File >>

70 // read account, name and balance from file 71 in. Client. File >> account >> name >> balance; 72 73 // display file contents (until eof) 74 while ( !in. Client. File. eof() ) { 75 76 // display record 77 if ( should. Display( request, balance ) ) 78 output. Line( account, name, balance ); 79 70 // read account, name and balance from file 81 in. Client. File >> account >> name >> balance; 82 } // end inner while 83 84 in. Client. File. clear(); // reset eof for next input 85 in. Client. File. seekg( 0 ); // move to beginning of file 86 request = get. Request(); // get additional request from user 87 } // end outer while ﺍﻻﻟﻜﺘﺮﻭﻧﻲ fig 14_2 ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 17

Updating Sequential-Access Files • Updating sequential files • Risk overwriting other data • Example:

Updating Sequential-Access Files • Updating sequential files • Risk overwriting other data • Example: change name "White" to "Worthington" • Old data 300 White 0. 00 400 Jones 32. 87 • Insert new data 300 Worthington 0. 00 300 White 0. 00 400 Jones 32. 87 Data gets overwritten 300 Worthington 0. 00 ones 32. 87 • Formatted text different from internal representation • Problem can be avoided, but awkward ﺍﻻﻟﻜﺘﺮﻭﻧﻲ ﻧﻈﺎﻡ ﺍﻟﻤﺤﺎﺿﺮﺍﺕ 18