270 likes | 398 Views
計算機概論實習 2007-03-09. Stream. Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer, etc.). in c scanf("%c", &ch); in c++ cin >> ch;. byte stream. byte stream. in c printf("%c", ch); in c++
E N D
Stream • Stream: sequence of bytes • Input: from device (keyboard, disk drive) to memory • Output: from memory to device (monitor, printer, etc.) in c scanf("%c", &ch); in c++ cin >> ch; byte stream byte stream in c printf("%c", ch); in c++ cout << ch; Computer
iostream Library Header Files • iostream library • Has header files with hundreds of I/O capabilities • <iostream.h> • Standard input (cin) • Standard output (cout) • Unbuffered error (cerr) • Buffered error (clog) • <iomanip.h> • Formatted I/O with parameterized stream manipulators • <fstream.h> • File processing operations
Standard input (cin) • Format input • cin >> grade • Need to include iostream and std:cin • Using >> operator • Normally skips whitespace (blanks, tabs, newlines) • Can change this • Returns 0 when EOF encountered • Otherwise, returns reference to object • State bits set if errors occur • Discussed in 12.7 and 12.8
Standard output (cout) • Format input • cout << grade • Need to include iostream and std:cout • Using << operator
Exmaple Code #include <iostream> usingnamespace std; void main(){ int i; cin >> i; cout << i; }
Multi-Input/Output int main(){ int a, b; cout << “Input two number: ”; cin >> a >> b; cout << “The inputs are ” << a << “ and ” << b; return 0; } Input two number: 4 7 The inputs are 4 and 7 space
Member Function: cin.get() • get(charRef) • With character reference argument • Gets only one character, stores in charRef • Returns reference to istream • If EOF, returns -1 • get(charArray, size) • get(charArray, size, delimiter) • Reads until size-1 characters read, or delimiter encountered • Default delimiter '\n' • Delimiter stays in input stream • Can remove with cin.get() or cin.ignore() • Makes array null-terminated
Member Function: cin.getline() • getline(array, size) • getline(array, size, delimiter) • Reads size-1 characters, or until delimiter found • Default delimiter '\n' • Removes delimiter from input stream • Puts null character at end of array
skip due to >> What Difference Among cin, cin.get(), and cin.getline() (1/3) int i; cin >> i; cout << “i = ” << i; i = 456 i = 456
Delimiter stays in input stream What Difference Among cin, cin.get(), and cin.getline() (2/3) char ch[10]; cin.get(ch, 10); cout << ch; ch[0] = ‘4’ ch[1] = ‘5’ ch[2] = ‘6’ ch[3] = 0 ch[0] = 0 Nothing is read
Removes delimiter from input stream What Difference Among cin, get(), and getline() (3/3) char ch[10]; cin.getline(ch, 10); cout << ch; ch[0] = ‘4’ ch[1] = ‘5’ ch[2] = ‘6’ ch[3] = 0 ch[0] = 0
Throw Away Delimiter • If use cin.get(), the delimiter will stay in the stream. Using the following method to Throw it Away. int main() { char buf[100]; char trash; while(cin.get(buf,100)) { // Get chars until ‘\n’ cin.get(trash); // Throw away the terminator cout << buf <<'\n'; // Add the ‘\n’ at the end } return 0; } Input : This is a test Output: This is a test
Stream Error States • Test state of stream using bits • eofbit set when EOF encountered • Function eof returns true if eofbit set • cin.eof() • failbit set when error occurs in stream • Data not lost, error recoverable • Function fail()returns true if set
Stream Error States • badbit set when data lost • Usually nonrecoverable • Function bad() • goodbit set when badbit, failbit and eofbit off • Function good()
Stream Error States • Member functions • rdstate() • Returns error state of stream • Can test for goodbit, badbit, etc. • Better to test using good(), bad() • clear() • Default argument goodbit • Sets stream to "good" state, so I/O can continue • Can pass other values • cin.clear( ios::failbit ) • Sets failbit • Name "clear" seems strange, but correct
int main(){ int integerValue; // display results of cin functions cout << "Before a bad input operation:" << "\ncin.rdstate(): " << cin.rdstate() << "\n cin.eof(): " << cin.eof() << "\n cin.fail(): " << cin.fail() << "\n cin.bad(): " << cin.bad() << "\n cin.good(): " << cin.good() << "\n\nExpects an integer, but enter a character: "; cin >> integerValue; // enter character value cout << endl;
// display results of cin functions after bad input cout << "After a bad input operation:" << "\ncin.rdstate(): " << cin.rdstate() << "\n cin.eof(): " << cin.eof() << "\n cin.fail(): " << cin.fail() << "\n cin.bad(): " << cin.bad() << "\n cin.good(): " << cin.good() << endl << endl; cin.clear(); // clear stream // display results of cin functions after clearing cin cout << "After cin.clear()" << "\ncin.fail(): " << cin.fail() << "\ncin.good(): " << cin.good() << endl; return 0; }
Before a bad input operation: cin.rdstate(): 0 cin.eof(): 0 cin.fail(): 0 cin.bad(): 0 cin.good(): 1 Expects an integer, but enter a character: c After a bad input operation: cin.rdstate(): 2 cin.eof(): 0 cin.fail(): 1 cin.bad(): 0 cin.good(): 0 After cin.clear() cin.fail(): 0 cin.good(): 1 Different error bit set leads to different value of rdstate()
Error Input Format – Simple Example int main () { int a; while (cin>>a){ cout<<"The input is:" <<a<<endl; } cout << "fail?" << cin.fail(); } 123 The input is:123 abc fail? 1
Error Input Format – Complex Example 123 The input is:123 ab fail?1 ab1 fail?1 123b The input is:123 fail?1 1 2 3 4
Explanation of Example - 1 • If there are not any error input, then program can get value from cin stream. 123 while (cin>>a) a=123
Explanation of Example - 2 • When the input appears format error, the failbit set. In other words, the return value of cin.fail() is 1. • But, the cin stream still keep the input value. • For example, when you input ab, it will be kept in the stream. while (cin>>a) ab a = -87361838 cin.fail() = 1
Explanation of Example - 4 • When you input 1ab, a will be 1 but the ab will be kept in the cin stream. • When you try to get from cin stream again, the failbit set. 1ab while (cin>>a) a=1 cin.fail() = 0 First time a=-8193787 cin.fail() = 1 Second time
Error Input Format – Another Example • Reset cin state and clear the stream. Then to request user to re-input with correct format (ex. Integer).
Need include limits Example Program // in order to prevent user from inputting incorrect format, clear stream void clear2eol(){ cin.ignore(numeric_limits<streamsize>::max(), '\n'); } int main(){ int a; while (cout << "Enter a number: " && !(cin >> a)) { cin.clear(); // reset cin iostate as goodbit clear2eol(); // clear the stream cout << "Wrong input. "; } clear2eol(); return 0; } Note that this code can not check the error such as “23abc”
Practice 1 (P1) • Try to write a program that request user to input the student’s information including name and score and output the information. • Please check that if the input of score is correct. If the input is not correct, then to request the user to re-input until the program gets a correct one.