12 highestGrade = grade; 13
14 cout << \15 } 16
17 cout << \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
图 3.11 流读取运算符在遇到文件结束符时返回false
3.4.2 成员函数get和getline
不带参数的get函数从指定的输入流中读取(输入)一个字符(包括空白字符),并返回该字符作为函数调用的值;当遇到输入流中的文件结束符时,该版本的get函数返回EOF。
图3.12中的程序把成员函数eof和get用于输入流cin,把成员函数put用于输出漉cout。程序首先输出了cin.eof()的值,输出值为0(false)表明没有在cin中遇到文件结束符。然后,程序让用户键入以文件结束符结尾的一行文本(在IBMPC兼容系统中,文件结束符用
的get成员函数,其返回值是所输入的字符。 1 // Fig. 3.12: figll_12.cpp
2 // Using member functions get, put and eof. 3 #include
7 char c; 8
9 cout << \
10 << \
-41-
11
12 while ( ( c = cin.get() ) != EOF ) 13 cout.put( c ); 14
15 cout << \
16 cout << \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
图 3.12 使用成员函数get.put和eof
带一个字符型参数的get成员函数自动读取输人流中的下一个字符(包括空白字符)。当遇到文件结束符时,该版本的函数返回0,否则返回对istream对象的引用,并用该引用再次调用get函数。
带有三个参数的get成员函数的参数分别是接收字符的字符数组、字符数组的大小和分隔符(默认值为'\\n')。函数或者在读取比指定的最大字符数少一个字符后结束,或者在遇到分隔符时结束。为使字符数组(被程序用作缓冲区)中的输入字符串能够结束,空字符会被插入到字符数组中。函数不把分隔符放到字符数组中,但是分隔符仍然会保留在输人流中。图3.13的程序比较了cin(与流读取运算符一起使用)和cin.get的输入结果。注意调用cin.get时未指定分隔符,因此用默认的'\\n'。
1 // Fig. 3.13: figll 13.cpp
2 // contrasting input of a string with cin and cin.get. 3 #include
7 const int SIZE = 80;
8 char buffer1[ SIZE ], buffer2[ SIZE ] ; 9
10 cout << \11 cin >> buffer1;
12 cout << \13 << buffer1 << \14
15 cin.get( buffer2, SIZE );
-42-
16 cout << \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
图 3.13 比较用cin和cin.get输入的字符串的结果
成员函数getline与带三个参数的get函数类似,它读取一行信息到字符数组中,然后插入一个空字符。所不同的是,getline要去除输入流中的分隔符(即读取字符并删除它),但是不把它存放在字符数组中。图3.14中的程序演示了用getline输入一行文本。 1 // Fig. 3.14: figll_14.cpp
2 // Character input with member function getline. 3 #include
5 int main() 6{
7 const SIZE = 80; 8 char buffe[ SIZE ]; 9
10 cout << \11 cin.getline( buffer, SIZE ); 12
13 cout << \14 return 0; 15 }
输出结果: Enter a sentence:
Using the getline member function
The sentence entered is:
Using the getline member function
图 3.14 用成员函数getline输入字符
-43-
3.5 成员函数read、gcount和write的无格式输入/输出
调用成员函数read、write可实现无格式输入/输出。这两个函数分别把一定量的字节写入字符数组和从字符数组中输出。这些字节都是未经任何格式化的,仅仅是以原始数据形式输入或输出。 例如:
char buffe[] =\; cout.write(buffer, 10 );
输出buffet的10个字节(包括终止cout和<<输出的空字符)。因为字符串就是第一个字符的地址,所以函数调用:
cout.write(\,10); 显示了字母表中的前10个字母。
成员函数read把指定个数的字符输入到字符数组中。如果读取的字符个数少于指定的数目,可以设置标志位failbit(见3.8节)。
成员函数gcount统计最后输入的字符个数。
图3.15中的程序演示了类istream中的成员函数read和gcount,以及类ostream中的成员函数write。程序首先用函数read向字符数组buffer中输入20个字符(输入序列比字符数组长),然后用函数gcount统计所输入的字符个数,最后用函数write输出buffer中的字符。
1 // Fig. 3.15: fig11_15.cpp
2 // Unformatted I/O with read, gcount and write. 3 #include
7 const int SIZE = 80; 8 char buffer[ SIZE ]; 9
10 cout << \11 cin.read( buffer, 20 );
12 cout << \13 cout.write( buffer, cin.gcount() ); 14 cout << endl; 15 return 0; 16 }
输出结果: Enter a sentence:
Using the read, write, and gcount member functions The sentence entered was: Using the read,write
图 3.15 成员函数read,gcount和write的无格式I/O
-44-
3.6 流操纵算子
C++提供了大量的用于执行格式化输入/输出的流操纵算子。流操纵算子提供了许多功能,如设置域宽、设置精度、设置和清除格式化标志、设置域填充字符、刷新流、在输出流中插入换行符并刷新该流、在输出流中插入空字符、跳过输入流中的空白字符等等。下面几节要介绍这些特征。
3.6.1 整数流的基数:流操纵算子dec、oct、hex和setbase
整数通常被解释为十进制(基数为10)整数。如下方法可改变流中整数的基数:插人流操纵算子hex可设置十六进制基数(基数为16)、插人流操纵算子oct可设置八进制基数(基数为8)、插人流操纵算子dec可恢复十进制基数。
也可以用流操纵算子setbace来改变基数,流操纵算于setbase带有一个整数参数10、8或16。因为流操纵算子setbase是带有参数的,所以也称之为参数化的流操纵算子。使用setbose或其他任何参数化的操纵算子都必须在程序中包含头文件iomanip.h。如果不明确地改变流的基数,流的基数是不变的。图3.16中的程序示范了流操纵算子hex、oct、dec和setbase的用法。 1 // Fig. 3.16: fig11_16.cpp
2 // Using hex, oct, dec and setbase stream manipulators. 3 #include
8 int n; 9
10 cout << \11 cin >> n; 12
13 cout << n << \14 << hex << n << '\\n'
15 << dec << n << \16 << oct << n << '\\n'
17 << setbase( 10 ) << n <<\18 << n << endl; 19
20 return 0; 21 }
输出结果:
Enter a decimal number: 20
-45-
信息学奥赛培训教程C++版



