Presentation is loading. Please wait.

Presentation is loading. Please wait.

計算機概論實習 2007-05-25. 2 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker 03124589... n-1 end-of-file marker 67 This is.

Similar presentations


Presentation on theme: "計算機概論實習 2007-05-25. 2 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker 03124589... n-1 end-of-file marker 67 This is."— Presentation transcript:

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

2 2 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker 03124589... n-1 end-of-file marker 67 This is a test HAHA!

3 3 File Processing include and 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 4 File-open Modes ModeDescription ios::appWrite all output to the end of the file. ios::ateOpen 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::inOpen a file for input. ios::outOpen a file for output. ios::trunc Discard the file ’ s contents if it exists (this is also the default action for ios::out) ios::binaryOpen a file for binary (i.e., nontext) input or output. ofstream outClientFile( "clients.dat", ios::out ); ofstream outClientFile( "clients.dat");

5 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 6 Example – Write to File #include #include // exit prototype using namespace 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 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 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 9 Example – Read from File #include #include // exit prototype using namespace 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 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 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 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 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 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 15 Examples fileObject.seekg(y, ios::end) Goes y bytes back from end seekp similar

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


Download ppt "計算機概論實習 2007-05-25. 2 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker 03124589... n-1 end-of-file marker 67 This is."

Similar presentations


Ads by Google