1 / 18

計算機概論實習 2007-05-25

計算機概論實習 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

malaya
Télécharger la présentation

計算機概論實習 2007-05-25

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 計算機概論實習2007-05-25

  2. ... 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!

  3. 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);

  4. File-open Modes ofstream outClientFile( "clients.dat", ios::out ); ofstream outClientFile( "clients.dat");

  5. 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)

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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. 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!

  14. 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!

  15. Examples • fileObject.seekg(y, ios::end) • Goes y bytes back from end • seekp similar

  16. 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.

  17. 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

  18. 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

More Related