下面我们要讨论流的输入,这是用流读取运算符(即重载的运算符>>)实现的。流读取运算符通常会跳过输人流中的空格、tab键、换行符等等的空白字符,稍后将介绍如何改变这种行为。当遇到输入流中的文件结束符时,流读取运算符返回0(false);否则,流读取运算符返回对调用该运算符的对象的引用。每个输入流都包含一组用于控制流状态(即格式化、出错状态设置等)的状态位。当输入类型有错时,流读取运算符就会设置输人流的failbit状态位;如果操作失败则设置badbit状态位,后面会介绍如何在I/O操作后测试这些状态位。11.7节和11.8节详细讨论了流的状态位。
图11.9中的范例程序用cin对象和重载的流读取运算符>>读取了两个整数。注意流读运算符可以连续使用。
// Fig. 11.9: figll_09.cpp // Calculating the sum of two integers input from the keyboard // with the cin oct and the stream-extraction operator. #include < iostream.h> int main() { int x, y; cout << "Enter two integers: "; cin >> x >> y; cout << "Sum of" << x << "and "<< y << "is:" << ( x + y ) << endl; return 0; }
输出结果:
Enter two integers: 30 92
Sum of 30 and 92 is: 122
图 11.9 计算用cin和流读取运算符从键盘输入的两个整数值的和
如果运算符>>和<<的优先级相对较高就会出现问题。例如,在图11.10的程序中,如果条件表达式没有用括号括起来,程序就得不到正确的编译(读者可以试一下)。
// Fig. 11.10: figlll0.cpp // Avoiding a precedence problem between the stream-insertion // operator and the conditional operator. // Need parentheses around the conditional expression. #include < iostream.h> int main() int x, y; cout << "Enter two integers: "; cin >> x >> y; cout << x << ( x == y ? "is" : "is not" ) << "equal to "<< y << endl; return 0; }
输出结果:
Enter two integers: 7 5
7 is not equal to 5
Enter two integers: 8 8
8 is equal to 8
图 11.10 避免在流插入运算符和条件运算符之间出现优先级错误
试图从类ostream的对象(或其它只有输出功能的流)中读取数据
试图把数据写入类istream的对象(或其它只有输入功能的流)
在流插入运算符<<或流读取运算符>>的优先级相对较高时没有用圆括号强制实现正确的计算顺序
我们通常在while循环结构的首部用流读取运算符输入一系列值。当遇到文件结束符时,读取运算符返回0false)。图11.11中的程序用于查找某次考试的最高成绩。假定事先不知道有多少个考试成绩,并且用户会输入表示成绩输入完毕的文件结束符。当用户输入文件结束符时,while循环结构中的条件(cin>>grade)将变为0(即false)。
提示用户如何从键盘结束输入时,让用户输入文件结束符结束输入而不是提示输入<ctrl>-d(UNIX与Macintosh所使用的)或<ctrl-z(PC与VAX所使用的)。
在图11. 11中,cin>>grade可以作为条件,因为基类ios(继承istream的类)提供一个重载的强制类型转换运算符,将流变成void*类型的指针。如果读取数值时发生错误或遇到文件结束符,则指针值为0。编译器能够隐式使用void*类型的强制转换运算符。
// Fig. 11.11: figll_ll.cpp // Stream-extraction operator returning false on end-of-file. #include < iostream.h> int main() { int grade, highestGrade = -1; cout << "Enter grade (enter end-of-file to end): "; while ( cin >> grade){ if ( grade > highestGrade ) highestGrade = grade; cout << "Enter grade (enter end-of-file to end): "; } cout << "\n\nHighest grade is: "<< highestGrade << endl; return 0; }
输出结果:
Enter grade (enter end-of-file to end): 67
Enter grade (enter end-of-file to end): 87
Enter grade (enter end of file to end): 73
Enter grade (enter end-of-file to end): 95
Enter grade (enter end-of-file to end): 34
Enter grade (enter end-of-file to end): 99
Entergrade (enter end-of-file to end):^ z
Heighest grade is: 99
图 11.11 流读取运算符在遇到文件结束符时返回false
不带参数的get函数从指定的输入流中读取(输入)一个字符(包括空白字符),并返回该字符作为函数调用的值;当遇到输入流中的文件结束符时,该版本的get函数返回EOF。
图11.12中的程序把成员函数eof和get用于输入流cin,把成员函数put用于输出漉cout。程序首先输出了cin.eof()的值,输出值为0(false)表明没有在cin中遇到文件结束符。然后,程序让用户键入以文件结束符结尾的一行文本(在IBMPC兼容系统中,文件结束符用<ctrl>-z表示;在UNIX和Macintosh系统中,文件结束符用<ctrl>-d表示)。程序逐个读取所输入的每个字符,井调用成员函数put将所读取的内容送往输出流cout。当遇到文件结束符时,while循环终止,输出cin.eof()的值,输出值为1(true)表示已接收到了cin中的文件结束符。注意,程序中使用了类istream中不带参数
的get成员函数,其返回值是所输入的字符。
// Fig. 11.12: figll_12.cpp // Using member functions get, put and eof. #include < iostream.h> int main() { char c; cout << "Before input, cin.eof() is "<< cin.eof() << "\nEnter a sentence followed by end-of-file:\n"; while ( ( c = cin.get() ) != EOF ) cout.put( c ); cout << "\nEOF in this system is: "<< c; cout << "\nAfter input, cin.eof() is "<< cin.eof() << endl; return 0; }
输出结果:
Before input, cin.eof() is 0
Enter a sentence followed by end-of-file:
Testing the get and put member functions^z
Testing the get and put member functions
EOF in this system is: -1
After input cin.eof() is 1
带一个字符型参数的get成员函数自动读取输人流中的下一个字符(包括空白字符)。当遇到文件结束符时,该版本的函数返回0,否则返回对istream对象的引用,并用该引用再次调用get函数。
带有三个参数的get成员函数的参数分别是接收字符的字符数组、字符数组的大小和分隔符(默认值为'\n')。函数或者在读取比指定的最大字符数少一个字符后结束,或者在遇到分隔符时结束。为使字符数组(被程序用作缓冲区)中的输入字符串能够结束,空字符会被插入到字符数组中。函数不把分隔符放到字符数组中,但是分隔符仍然会保留在输人流中。图11.13的程序比较了cin(与流读取运算符一起使用)和cin.get的输入结果。注意调用cin.get时未指定分隔符,因此用默认的'\n'。
// Fig. 11.13: figll 13.cpp // contrasting input of a string with cin and cin.get. #include < iostream.h> int main() { const int SIZE = 80; char buffer1[ SIZE ], buffer2[ SIZE ] ; cout << "Enter a sentence:\n"; cin >> buffer1; cout << "\nThe string read with cin was:\n" << buffer1 << "n\n"; cin.get( buffer2, SIZE ); cout << "The string read with cin.get was:\n" << buffer2 << endl; return 0; }
输出结果:
Enter a sentence:
Contrasting string input with cin and cin.get
The string read with cin was:
Contrasting
The string read with cin.get was:
string input with cin and cin.get
图 11.13 比较用cin和cin.get输入的字符串的结果
成员函数getline与带三个参数的get函数类似,它读取一行信息到字符数组中,然后插入一个空字符。所不同的是,getline要去除输入流中的分隔符(即读取字符并删除它),但是不把它存放在字符数组中。图11.14中的程序演示了用getline输入一行文本。
// Fig. 11.14: figll_14.cpp // Character input with member function getline. #include < iostream.h> int main() { const SIZE = 80; char buffe[ SIZE ]; cout << "Enter a sentence:\n"; cin.getline( buffer, SIZE ); cout << "\nThe sentence entered is:\n" << buffer << endl; return 0; }
输出结果:
Enter a sentence:
Using the getline member function
The sentence entered is:
Using the getline member function
图 11.14 用成员函数getline输入字符
成员函数ignore用于在需要时跳过流中指定数量的字符(默认个数是1),或在遇到指定的分隔符(默认分隔符是EOF,使得ignore在读文件的时候跳过文件末尾)时结束。
成员函数putback将最后一次用get从输人流中提取的字符放回到输入流中。对于某些应用程序.该函数是很有用的。例如,如果应用程序需要扫描输入流以查找用特定字符开始的域,那么当输入该字符时,应用程序要把该字符放回到输入流中,这样才能使该字符包含到要被输入的数据中。
成员函数peek返回输入流中的下一个字符,但并不将其从输入流中删除。
C++提供类型安全的I/O。重载运算符<<和>>是为了接收指定类型的数据。如果接收了非法的数据类型,那么系统就会设置各种错误标志,用户可通过测试这些标志判断I/O操作的成功与失败。这种处理方法能够使程序保持控制权。11.8节要讨论这些错误标志。