📄 11章 c++输入输出流.txt
字号:
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节要讨论这些错误标志。
11.5 成员函数read、gcount和write的无格式输入/输出
调用成员函数read、write可实现无格式输入/输出。这两个函数分别把一定量的字节写入字符数组和从字符数组中输出。这些字节都是未经任何格式化的,仅仅是以原始数据形式输入或输出。
例如:
char buffe[] ="HAPPY BIRTHDAY";
cout.write(buffer, 10 );
输出buffet的10个字节(包括终止cout和<<输出的空字符)。因为字符串就是第一个字符的地址,所以函数调用:
cout.write("ABCDEFGHIJKLMNOPQRSTUVWXYZ",10);
显示了字母表中的前10个字母。
成员函数read把指定个数的字符输入到字符数组中。如果读取的字符个数少于指定的数目,可以设置标志位failbit(见11.8节)。
成员函数gcount统计最后输入的字符个数。
图11.15中的程序演示了类istream中的成员函数read和gcount,以及类ostream中的成员函数write。程序首先用函数read向字符数组buffer中输入20个字符(输入序列比字符数组长),然后用函数gcount统计所输入的字符个数,最后用函数write输出buffer中的字符。
1 // Fig. 11.15: fig11_15.cpp
2 // Unformatted I/O with read, gcount and write.
3 #include <iostream.h>
4
5 int main()
6 {
7 const int SIZE = 80;
8 char buffer[ SIZE ];
9
10 cout << "Enter a sentence:\n";
11 cin.read( buffer, 20 );
12 cout << "\nThe sentence entered was:\n";
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
图 11.15 成员函数read,gcount和write的无格式I/O
11.6 流操纵算子:
C++提供了大量的用于执行格式化输入/输出的流操纵算子。流操纵算子提供了许多功能,如设置域宽、设置精度、设置和清除格式化标志、设置域填充字符、刷新流、在输出流中插入换行符并刷新该流、在输出流中插入空字符、跳过输入流中的空白字符等等。下面几节要介绍这些特征。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -