当前位置: 首页 > 文档资料 > C++大学教程 >

11.4 输入流

优质
小牛编辑
122浏览
2023-12-01

下面我们要讨论流的输入,这是用流读取运算符(即重载的运算符>>)实现的。流读取运算符通常会跳过输人流中的空格、tab键、换行符等等的空白字符,稍后将介绍如何改变这种行为。

当遇到输入流中的文件结束符时,流读取运算符返回0(false);否则,流读取运算符返回对调用该运算符的对象的引用。每个输入流都包含一组用于控制流状态(即格式化、出错状态设置等)的状态位。当输入类型有错时,流读取运算符就会设置输人流的failbit状态位;如果操作失败则设置badbit状态位,后面会介绍如何在I/O操作后测试这些状态位。11.7节和11.8节详细讨论了流的状态位。

11.4.1 流读取运算符

图11.9 中的范例程序用cin对象和重载的流读取运算符>>读取了两个整数。注意流读运算符可以连续使用。

1 // Fig. 11.9: figll_09.cpp
2 // Calculating the sum of two integers input from the keyboard
3 // with the cin oct and the stream-extraction operator.
4 #include <iostream.h>
5
6 int main()
7{
8 int x, y;
9
10 cout << "Enter two integers: ";
11 cin >> x >> y;
12 cout << "Sum of" << x << "and "<< y << "is:"
13 << ( x + y ) << endl;
14
15 return 0;
16 }

输出结果:

Enter two integers: 30 92
Sum of 30 and 92 is: 122

图 11.9 计算用cin和流读取运算符从键盘输入的两个整数值的和

如果运算符>>和<<的优先级相对较高就会出现问题。例如,在图 11.10 的程序中,如果条件表达式没有用括号括起来,程序就得不到正确的编译(读者可以试一下)。

1 // Fig. 11.10: figlll0.cpp
2 // Avoiding a precedence problem between the stream-insertion
3 // operator and the conditional operator.
4 // Need parentheses around the conditional expression.
5 #include <iostream.h>
6
7 int main()
8
9 int x, y;
10
11 cout << "Enter two integers: ";
12 cin >> x >> y;
13 cout << x << ( x == y ? "is" : "is not" )
14 << "equal to "<< y << endl;
15
16 return 0;
17 }

输出结果:

Enter two integers: 7 5
7 is not equal to 5

Enter two integers: 8 8
8 is equal to 8

图 11.10 避免在流插入运算符和条件运算符之间出现优先级错误

常见编程错误 11.1
试图从类ostream的对象(或其它只有输出功能的流)中读取数据

常见编程错误 11.2
试图把数据写入类istream的对象(或其它只有输入功能的流)

常见编程错误 11.3
在流插入运算符 << 或流读取运算符>>的优先级相对较高时没有用圆括号强制实现正确的计算顺序

我们通常在 while 循环结构的首部用流读取运算符输入一系列值。当遇到文件结束符时,读取运算符返回0false)。图11.11中的程序用于查找某次考试的最高成绩。假定事先不知道有多少个考试成绩,并且用户会输入表示成绩输入完毕的文件结束符。当用户输入文件结束符时,while循环结构中的条件(cin>>grade)将变为0(即false)。

可移植性提示 11.1
提示用户如何从键盘结束输入时,让用户输入文件结束符结束输入,而不是提示输入<ctrl>-d(UNIX与Macintosh所使用的)或<ctrl-z(PC与VAX所使用的)。

在图 11. 11 中,cin>>grade 可以作为条件,因为基类 ios(继承 istream 的类)提供一个重载的强制类型转换运算符,将流变成 void* 类型的指针。如果读取数值时发生错误或遇到文件结束符,则指针值为0。编译器能够隐式使用 void* 类型的强制转换运算符。

1 // Fig. 11.11: figll_ll.cpp
2 // Stream-extraction operator returning false on end-of-file.
3 #include <iostream.h>
4
5 int main()
6 {
7 int grade, highestGrade = -1;
8
9 cout << "Enter grade (enter end-of-file to end): ";
10 while ( cin >> grade){
11 if ( grade > highestGrade )
12 highestGrade = grade;
13
14 cout << "Enter grade (enter end-of-file to end): ";
15 }
16
17 cout << "\n\nHighest grade is: "<< highestGrade << endl;
18 return 0;
19 }

输出结果:

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

11.4.2 成员函数 get 和 getline

不带参数的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成员函数,其返回值是所输入的字符。

1 // Fig. 11.12: figll_12.cpp
2 // Using member functions get, put and eof.
3 #include <iostream.h>
4
5 int main()
6 {
7 char c;
8
9 cout << "Before input, cin.eof() is "<< cin.eof()
10 << "\nEnter a sentence followed by end-of-file:\n";
11
12 while ( ( c = cin.get() ) != EOF )
13 cout.put( c );
14
15 cout << "\nEOF in this system is: "<< c;
16 cout << "\nAfter input, cin.eof() is "<< cin.eof() << endl;
17 return 0;
18 }

输出结果:

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

图 11.12 使用成员函数 get.put 和 eof

带一个字符型参数的get成员函数自动读取输人流中的下一个字符(包括空白字符)。当遇到文件结束符时,该版本的函数返回0,否则返回对istream对象的引用,并用该引用再次调用get函数。

带有三个参数的get成员函数的参数分别是接收字符的字符数组、字符数组的大小和分隔符(默认值为'\n')。函数或者在读取比指定的最大字符数少一个字符后结束,或者在遇到分隔符时结束。为使字符数组(被程序用作缓冲区)中的输入字符串能够结束,空字符会被插入到字符数组中。函数不把分隔符放到字符数组中,但是分隔符仍然会保留在输人流中。图 11.13 的程序比较了cin(与流读取运算符一起使用)和cin.get的输入结果。注意调用cin.get时未指定分隔符,因此用默认的'\n'。

1 // Fig. 11.13: figll 13.cpp
2 // contrasting input of a string with cin and cin.get.
3 #include <iostream.h>
4
5 int main()
6{
7 const int SIZE = 80;
8 char buffer1[ SIZE ], buffer2[ SIZE ] ;
9
10 cout << "Enter a sentence:\n";
11 cin >> buffer1;
12 cout << "\nThe string read with cin was:\n"
13 << buffer1 << "n\n";
14
15 cin.get( buffer2, SIZE );
16 cout << "The string read with cin.get was:\n"
17 << buffer2 << endl;
18
19 return 0;
20 }

输出结果:

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 输入一行文本。

1 // Fig. 11.14: figll_14.cpp
2 // Character input with member function getline.
3 #include <iostream.h>
4
5 int main()
6{
7 const SIZE = 80;
8 char buffe[ SIZE ];
9
10 cout << "Enter a sentence:\n";
11 cin.getline( buffer, SIZE );
12
13 cout << "\nThe sentence entered is:\n" << buffer << endl;
14 return 0;
15 }

输出结果:

Enter a sentence:
Using the getline member function

The sentence entered is:
Using the getline member function

图 11.14 用成员函数 getline 输入字符

11.4.3 istream 类中的其他成员函数(peek、putback 和 ignore)

成员函数 ignore 用于在需要时跳过流中指定数量的字符(默认个数是1),或在遇到指定的分隔符(默认分隔符是 EOF,使得 ignore 在读文件的时候跳过文件末尾)时结束。
成员函数 putback 将最后一次用 get 从输人流中提取的字符放回到输入流中。

对于某些应用程序.该函数是很有用的。例如,如果应用程序需要扫描输入流以查找用特定字符开始的域,那么当输入该字符时,应用程序要把该字符放回到输入流中,这样才能使该字符包含到要被输入的数据中。

成员函数 peek 返回输入流中的下一个字符,但并不将其从输入流中删除。

11.4.4 类型安全的 I/O

C++ 提供类型安全的 I/O。重载运算符 << 和 >> 是为了接收指定类型的数据。如果接收了非法的数据类型,那么系统就会设置各种错误标志,用户可通过测试这些标志判断 I/O 操作的成功与失败。这种处理方法能够使程序保持控制权。11.8 节要讨论这些错误标志。