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 these stream template specializations. A 9. File Processing 0001 Jane Gloria Doe accnt name balance 0023 Gloria record Gloria field G byte bit file Data Hierarchy data base 0 struct or class in C++ and must be included.
2008YeungNam Univ. SE Lab. #include using std::cout; using std::cin; using std::ios; #include using std::ofstream; struct Client { int a; // account char n[20]; // name double b; // balance }; int main() { ofstream osf("C:\\client.txt", ios::out); if ( !osf ) return 1; Client c; cout << "Enter account name balance:\n? "; while ( cin >> c.a >> c.n >> c.b ) { osf<<c.a<<' '<<c.n<<' '<<c.b<<'\n'; cout << "? "; } osf.close(); // optional return 0; } csf.cpp 2 C++ imposes no structure, such as “record”, on a file. Creating a Sequential File File I/O “ lpt1: ” for printer default path: Current project’s path Open mode ); Default open mode for ofstream 9. File Processing A ofstream osf; osf.open("C:\\client.txt"); Enter account name balance: ? 0001 Jane ? 0023 Gloria ? 0035 Doe 89.00^z ^z
2008YeungNam Univ. SE Lab. 3 File Open Modes File I/O opens a file for binary input or output ios::binary discards the file’s contents if it exist. This is the default action for ios::out ios::trunc opens a file for output ios::out opens a file for input ios::in opens a file for output and move to the end of the file. Data can be written anywhere in the file. ios::ate writes all output to the end of file. ios::app DescriptionMode 9. File Processing A
2008YeungNam Univ. SE Lab. 4 Reading a Sequential File File I/O void putClient( const Client& c ) { static int title = 1; if ( title && title-- ) cout << "Acnt...Name Balance\n" << setiosflags(ios::fixed|ios::showpoint); cout<< setw(4) << right << setfill('0') << c.a << ' ' << setw(13) << left << setfill(' ') << c.n << setw(7) << right << setprecision(2)<< c.b <<'\n'; } int main( ) { ifstream isf; isf.open( "C:\\client.txt", ios::in ); if ( !isf ) { cerr << "Open failed...\n"; return 1; } Client c; while ( isf >> c.a >> c.n >> c.b ) putClient( c ); return 0; } #include using std::cout; using std::cin; using std::cerr; using std::ios; #include using std::ifstream; #include using std::setiosflags; using std::setprecision; using std::setw; using std::setfill; using std::left; using std::right; struct Client { int a; char n[20]; double b; }; 9. File Processing A rsf.cpp Acnt...Name Balanc e 0001 Jane Gloria Doe 89.00
2008YeungNam Univ. SE Lab. #include using std::cout; using std::cin; using std::ios; #include using std::ofstream; struct Client { int a; char n[20]; double b; }; int main() { ofstream asf( "C:\\client.txt", ios::app ); if ( !asf ) return 1; Client c; cout << "Enter account name balance:\n? "; while ( cin >> c.a >> c.n >> c.b ) { asf << c.a << ' ' << c.n << ' ' << c.b << '\n'; cout << "? "; } return 0; } asf.cpp 5 Appending a Sequential File File I/O 9. File Processing A Enter account name balance: ? 0047 Jack ? 0051 Thomas ^z ^z
2008YeungNam Univ. SE Lab. 69. File Processing File Position Pointers File I/O istream is long cur_file_pos = is.tellg() is.seekg(5, ios::beg) is.seekg(3, ios::cur) “tell get” file position pointer is.seekg(4, ios::end) is.seekg(0) is.seekg(0, ios::end) default seek direction A The byte numbers of the next byte in the file to be read or written The same operations can be performed with ostream MFs tellp() and seekp() “seek put” file position pointer
2008YeungNam Univ. SE Lab. #include using std::cout; #include using std::ifstream; int main() { int x, n, s; // number, count, sum double m; // mean ifstream nf("c:\\numbers.txt"); if ( !nf ) return 1; n = s = 0; while ( nf >> x ) ++n, s += x; m = double(s) / n; nf.clear(); // reset EOFBIT nf.seekg(0); // move to beginning of file n = 0; while ( nf >> x ) if ( x > m ) ++n; cout << n << " number" << (n<2?"":"s") << " exceed" << (n<2?"s":"") << " the average " << m << '\n'; return 0; } avg.cpp 7 Count the numbers exceeding their average value. File I/O 9. File Processing A Reading a File Twice 12 numbers exceed the average 23.5
2008YeungNam Univ. SE Lab. 8 File I/O 9. File Processing A Updating Sequential Access Files insert delete
2008YeungNam Univ. SE Lab : : : : : 46 : 50 : : 99 File I/O 9. File Processing A Random-Access File analogous to an array of records on a disc. Jane e+02Gloria e+02Doe e+02Jack e+02 Thomas e
2008YeungNam Univ. SE Lab. 10 File I/O 9. File Processing A Creating a Random-Access File cout << "Account[1.." << capacity << "] Name Balance:\n? "; while ( cin >> a >> c.n >> c.b ) { if ( a 100 ) continue; orf.seekp( (a-1)*sz ); orf.write( reinterpret_cast< const char*>(&c), sz ); cout << "? "; } orf.close(); return 0; } #include using std::cout; using std::cin; using std::ios; #include using std::ofstream; struct Client { char n[20]; double b; }; const int sz = sizeof( Client ); int main() { char file[ ] = "C:\\client.bin"; const int capacity = 100; ofstream orf( file, ios::binary ); if ( !orf ) return 1; int a; // account number Client c = { "", 0.0 }; for ( a = 0; a<capacity; ++a ) orf.write( reinterpret_cast< const char*>(&c), sz ); orf.close(); orf.open( file, ios::binary ); if ( !orf ) return 2; crf.cpp Account[1..100] Name Balance: ? 35 Doe ? 1 Jane ? 51 Thomas ? 23 Gloria ? 47 Jack 30.80^z ^z
2008YeungNam Univ. SE Lab. #include using std::cout; using std::ios; #include using std::ifstream; struct Client { char n[20]; double b; }; const int sz = sizeof( Client ); int main( ) { char file[ ] = "C:\\client.bin"; const int capacity = 100; ifstream irf( file, ios::binary ); if ( !irf ) return 1; int a; // account number Client c; for ( a=1; a<=capacity; ++a ) { irf.read( reinterpret_cast (&c), sz ); if ( !irf ) break; if ( c.n[0] != '\0' ) cout << a <<' '<<c.n<<' '<< c.b << '\n'; } irf.close(); return 0; } rrf.cpp 11 File I/O 9. File Processing A Reading a Random-Access File Sequentially fstream fstream irf( file, ios::binary|ios::in ); 1 Jane Gloria Doe Jack Thomas
2008YeungNam Univ. SE Lab. 12 File I/O 9. File Processing A Updating Random-Access File Randomly [i|d|r|p|e] Acc ? i 3 name balance? Anny ? r 23 Gloria => Gloria ? d 47 ? p 1 Jane ? p 3 Anny ? p 23 Gloria ? p ? e ^z ^z #include using namespace std; #define Rec( t ) reinterpret_cast (&t), sz struct Client { char n[20]; double b; }; const int sz = sizeof( Client ); int main() { char file[] = "C:\\client.bin"; const int capacity = 100; fstream urf( file, ios::in|ios::out ); if ( !urf ) return 1; Client c; Client blank = { "", 0.0 }; int acc; // account char ucc; // update control code long fpp; // file position pointer urf.cpp
2008YeungNam Univ. SE Lab. 13 File I/O cout << setiosflags( ios::fixed ) << setprecision( 2 ); cout << "[i|d|r|p|e] acc"; while ( cout > ucc ) { cin >> acc; // need to check fpp = (acc-1)*sz; switch ( tolower(ucc) ) { case 'i': // insert cout << "name balance? "; cin >> c.n >> c.b; urf.seekp( fpp ).write( Rec(c) ); break; case 'd': // delete urf.seekp( fpp ).write( Rec(blank) ); break; case 'r': // replace urf.seekg( fpp ).read( Rec(c) ); cout "; cin >> c.n >> c.b; urf.seekp( fpp ).write( Rec(c) ); break; case 'p': // print urf.seekg( fpp ).read( Rec(c) ); cout << c.n << " " << c.b << "\n"; break; 9. File Processing A case 'e': // end return 0; } return 0; } [i|d|r|p|e] Acc ? i 3 name balance? Anny ? r 23 Gloria => Gloria ? d 47 ? p 1 Jane ? p 3 Anny ? p 23 Gloria ? p ? e ^z
2008YeungNam Univ. SE Lab. 14 File I/O of Objects File I/O 9. File Processing #include using std::cin; using std::cout; using std::istream; using std::ostream; #include class Dog { public: Dog() : name(new char(0)), age(0) { } Dog(char* s, int a=0) : age(a) { name = strdup( s ); } ~Dog() { delete []name; } friend istream& operator >>( istream&, Dog& ); friend ostream& operator <<( ostream&, const Dog& ); private: char* name; int age; }; Dog.h
2008YeungNam Univ. SE Lab. #include "Dog.h" #include istream& operator>>( istream& is, Dog& d ) { char s[30], c; while ( isspace( is.peek() ) ) is.get(c); // skip leading white spaces is.getline( s, 30, ',' ); is >> d.age; d.name = strdup(s); return is; } ostream& operator<<(ostream& os,const Dog& d) { if ( strlen(d.name) ) os << d.name << ", " << d.age; return os; } Dog.cpp #include "Dog.h" #include using std::ofstream; using std::ifstream; using std::ios; int main() { ofstream osf("C:\\Dog.txt"); if ( !osf ) return 1; Dog d; while ( cin >> d ) osf << d; osf.close(); ifstream isf("C:\\Dog.txt"); if ( !isf ) return 2; while ( isf >> d ) cout << d << '\n'; isf.close(); return 0; } Drv.cpp 15 File I/O 9. File Processing Lucky, 5 Minky, 2 Frisky, 3 ^z Lucky, 5 Minky, 2 Frisky, 3 Press any