1、I/O流的概念I/O流的概念I/O流的概念I/O流的概念输出流输出流输出流输出流输出流输出流输出流输出流输出流输出流ios:skipws Skip white space on input.ios:left Left-align values;pad on the right with the fill character.ios:right Right-align values;pad on the left with the fill character(default alignment).ios:internal Add fill characters after any leadin
2、g sign or base indication,but before the value.ios:dec Format numeric values as base 10(decimal)(default radix).ios:oct Format numeric values as base 8(octal).ios:hex Format numeric values as base 16(hexadecimal).ios:showbase Display numeric constants in a format that can be read by the C+compiler.i
3、os:showpoint Show decimal point and trailing zeros for floating-point values.ios:uppercase Display uppercase A through F for hexadecimal values and E for scientific values.ios:showpos Show plus signs(+)for positive values.ios:scientific Display floating-point numbers in scientific format.ios:fixed D
4、isplay floating-point numbers in fixed format.ios:unitbuf Cause ostream:osfx to flush the stream after each insertion.By default,cerr is unit buffered.ios:stdio Cause ostream:osfx to flush stdout and stderr after each insertion.例例 设置对齐方式设置对齐方式#include#includevoid main()double values=1.23,35.36,653.7
5、,4358.24;char*names=Zoot,Jimmy,Al,Stan;for(int i=0;i4;i+)coutsetiosflags(ios:left)/左对齐左对齐 setw(6)namesi resetiosflags(ios:left)/恢复默认恢复默认 setw(10)valuesIendl;输出流程序运行结果:Zoot 1Jimmy 4e+001Al 7e+002Stan 4e+003若改为若改为setprecision(3),即即3位有效数字,结果位有效数字,结果成为:成为:Zoot 1.23Jimmy 35.4Al 654Stan 4.36e+003原数据为:1.23
6、,35.36,653.7,4358.24不必使用不必使用resetprecision,可直接修改精度。,可直接修改精度。输出流程序运行结果:234 eaea 352352 234程序运行结果:20H 21H!22H 23H#24H$25H%26H&27H 28H(29H)2aH*2bH+2cH,2dH-2eH.2fH/30H 0 31H 1 32H 2 33H 3 34H 4 35H 5 36H 6 37H 738H 8 39H 9 3aH:3bH;3cH 3fH?40H 41H A 42H B 43H C 44H D 45H E 46H F 47H G48H H 49H I 4aH J 4b
7、H K 4cH L 4dH M 4eH N 4fH O50H P 51H Q 52H R 53H S 54H T 55H U 56H V 57H W58H X 59H Y 5aH Z 5bH 5cH 5dH 5eH 5fH _60H 61H a 62H b 63H c 64H d 65H e 66H f 67H g68H h 69H i 6aH j 6bH k 6cH l 6dH m 6eH n 6fH o70H p 71H q 72H r 73H s 74H t 75H u 76H v 77H w78H x 79H y 7aH z 7bH 7cH|7dH 7eH 输出流输出流输出流输出流程序
8、运行结果:文件date.dat的内容是:06 00 00 00 0A 00 00 00-5C 00 00 00正好是十进制数正好是十进制数6、10、92的二进制表示,每个数的二进制表示,每个数4个字节。个字节。如果以文本方式打开,程序运行结果:文件date.dat的内容是:06 00 00 00 0D 0A 00 00-00 5C 00 00 00十进制数十进制数10前面增加了一个前面增加了一个0D,即换行符的即换行符的ASCII码,其余不变。可见并不能直接按文本方式输出。码,其余不变。可见并不能直接按文本方式输出。而且使而且使10的输出(认为是的输出(认为是ASCII码码0A)变为回车换变为
9、回车换行符。行符。输出流输出流输出流输入流输入流输入流#includevoid main()char ch;while(ch=cin.get()!=EOF)cout.put(ch);运行时输入:运行时输入:abc xyz 123则输出:则输出:abc xyz 123注:先输入到一个缓冲区,再由注:先输入到一个缓冲区,再由get读入,读入,put输出。输出。输入流#includevoid main()char ch;while(ch=cin.get()!=EOF)cout.put(ch);运行时输入:运行时输入:abc xyz 123则输出:则输出:abc xyz 123注:先输入到一个缓冲区,
10、再由注:先输入到一个缓冲区,再由get读入,读入,put输出。输出。例例 读入一串字符到读入一串字符到line数组,以数组,以t终止终止#include void main()char line100;cout“Type a line terminated by t”endl;cin.getline(line,100,t);coutline;例例 从一个从一个payroll文件读一个二进制记录到一个结构中文件读一个二进制记录到一个结构中#include#include#includevoid main()struct double salary;char name23;employee;ifs
11、tream is(payroll,ios:binary|ios:nocreate);if(is)is.read(char*)&employee,sizeof(employee);coutemployee.name employee.salaryendl;else coutERROR:Cannot open file payroll.endl;输入流例例 读一个文件并显示出其中空格的位置读一个文件并显示出其中空格的位置#includevoid main()char ch;ifstream tfile(payroll,ios:binary|ios:nocreate);if(tfile)while(
12、tfile.good()streampos here=tfile.tellg();tfile.get(ch);if(ch=)coutnPositionhereis a space;else coutERROR:Cannot open file payrollendl;文件文件payroll的内容:的内容:123 44 5555 6 ios:goodPosition3 is a spacePosition6 is a spacePosition11 is a spacePosition12 is a spacePosition13 is a space.ios:goodint good()const;Return ValueReturns a nonzero value if all error bits are clear.Note that the good member function is not simply the inverse of the bad function.