180 likes | 318 Views
計算機概論實習 2007-05-25. n-1. 7. 6. 9. 8. 2. 4. 5. 1. 0. 3. end-of-file marker. Files and Streams. C++ views file as sequence of bytes Ends with end-of-file marker. This is a test HAHA!. File Processing. include <iostream> and <fstream> To open file, create objects
E N D
... n-1 7 6 9 8 2 4 5 1 0 3 end-of-file marker ... Files and Streams • C++ views file as sequence of bytes • Ends with end-of-file marker This is a test HAHA!
File Processing • include <iostream> and <fstream> • 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 outClientFile( "filename", fileOpenMode ); • To attach a file later Ofstream outClientFile; outClientFile.open( "filename", fileOpenMode);
File-open Modes ofstream outClientFile( "clients.dat", ios::out ); ofstream outClientFile( "clients.dat");
Operations • Writing to file (just like cout) • outClientFile << myVariable • Closing file • outClientFile.close() • Automatically closed when destructor called • Reading from files • inClientFile >> myVariable • Stops when EOF found (gets value 0)
Example – Write to File #include <iostream> #include <fstream> #include <cstdlib> // exit prototype usingnamespace std; int main() { // ofstream constructor opens file ofstream outClientFile( "clients.dat", ios::out ); // exit program if unable to create file if ( !outClientFile ) { // overloaded ! operator cout << "File could not be opened" << endl; exit( 1 ); } // end if
Example – Write to File 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 ) { outClientFile << account << ' ' << name << ' ' << balance << endl; cout << "? "; } // end while return 0; // ofstream destructor closes file } // end main
Example – Write to File Enter the account, name, and balance. Enter end-of-file to end input. ? 100 Jones 24.98 ? 200 Doe 345.67 ? 300 White 0.00 ? 400 Stone -42.16 ? 500 Rich 224.62 ? ^Z
Example – Read from File #include <iostream> #include <fstream> #include <iomanip> #include <cstdlib> // exit prototype usingnamespace std; int main() { // ifstream constructor opens the file ifstream inClientFile( "clients.dat", ios::in ); // exit program if ifstream could not open file if ( !inClientFile ) { cout << "File could not be opened" << endl; exit( 1 ); } // end if
Example – Read from File int account; char name[ 30 ]; double balance; cout << left << setw( 10 ) << "Account" << setw( 13 ) << "Name" << "Balance" << endl << fixed << showpoint; // display each record in file while ( inClientFile >> account >> name >> balance ) { cout << left << setw( 10 ) << account << setw( 13 ) << name << setw( 7 ) << setprecision( 2 ) << right << balance << endl; } return 0; // ifstream destructor closes the file } // end main
Example – Read from File Account Name Balance 100 Jones 24.98 200 Doe 345.67 300 White 0.00 400 Stone -42.16 500 Rich 224.62
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
Examples • fileObject.seekg(0) • Goes to front of file (location 0) because ios::beg is default • fileObject.seekg(n) • Goes to nth byte from beginning • Example: n = 8 This is a test HAHA! This is a test HAHA!
Examples • fileObject.seekg(n, ios::cur) • Goes n bytes forward • Example: ios:cur = 8, n = 2 • fileObject.seekg(0, ios::cur) • Goes to last byte This is a test HAHA!
Examples • fileObject.seekg(y, ios::end) • Goes y bytes back from end • seekp similar
Practice7 (P7) • A student file stores the student’s name and the grade of Chinese, Mathematics, and English. • Please provide a program that read the content of student file and print out. Then after computing the average grade, you must store all the information in another file. The information contains student’s name, the grade of the three courses, and finally the average grade.
Example – Read File • The content student file Judy 30 30 40 May 40 40 30 Gary 80 90 50 • To print above information on the screen 姓名 國文 英文 數學 Judy 30 30 40 May 40 40 30 Gary 80 90 50
Example – Write to File • Store all information in another file (e.g. student-1.txt) Judy 30 30 40 33.33 May 40 40 30 36.67 Gary 80 90 50 73.33