资源描述
F:\wenkufile2\2024-11\24\3dc03d31-5a59-4e59-a6b7-e6094d5ef48c\53947e9c02b496ffc041b1ce413c7589.doc
第十三章 C++语言I/O流类
13.1 概述
13.1.1 流(stream)
逻辑设备流和信息流
信息流分两类:文本流和二进制流。
文本流和二进制流区别:占用内存不同,表达形式不同。
13.1.2 文件
设备文件和信息文件。
打开文件建立流与文件的关联,关闭文件将断开流与文件的联系。
13.1.3 缓冲与缓冲区
13.2 C++的基本流类体系
13.2.1 基本流类体系
ios->streambuf ->istream iostream ->ostream
13.2.2 预定义的流及运算符(标准流)
extern istream cin <类名> <对象> 输入流建立与控制台输入文件(键盘)的联系
extern ostream cout 输出流建立与控制台输出文件(显示器)的联系
13.2.3 流的格式化输入输出( I/O)
13.2.3.1 格式控制字
enum { //枚举类型
dec //将数据转成十进值
oct //将数据转成八进值
hex //将数据转成十六进值
showbase(n) //输出带有表示数制的字符(n只能是8,10,16之一)
setfill(c ) //填充字符
setprecision(n) //设置十进制有效位,指数输出时设置小数位数
setw(n) //设置字段宽度
setiosflags(ios::skipws) //跳过前导空白字符
setiosflags(ios: left) //输出左对齐
setiosflags(ios: right)
setiosflags(ios::fixed) //固定小数输出
setiosflags(ios::scientific) //指数输出
setiosflags(ios::uppercase) //大写字母指数输出
setiosflags(ios:: showpos) //输出后嗣输出‘+’
showpoint //输出实数必须带有小数点
}
说明:在cout中直接使用。
13.2.3.2 格式控制函数
是类ios 中的成员函数
ios::: flags(), 取标志中的指定位
ios::: setf(), 设置标志中的指定位
ios::: unsetf() 取消标志中的指定位
ios::: dec //将数据转成十进值
ios::: oct //将数据转成八进值
ios::: hex //将数据转成十六进值
ios::: showbase(n) //输出带有表示数制的字符(n只能是8,10,16之一)
ios::scientific //指数输出
ios::uppercase //大写字母指数输出
ios:: showpos //输出后嗣输出‘+’
ios: left //输出左对齐
ios: right
ios::fixed //固定小数输出
ios::scientific) //指数输出
ios::uppercase //大写字母指数输出
说明:在cout.setf( )中使用,设置有效
cout.useetf( ) 中使用,设置无效
例: 使用设置标志字的例子
void main()
{cout<<”input integer”;
int num;
cin>>num;
cout<<”Dec:”<<num<<’\t’; //默认十进制
cout.setf(ios::showbase | ios::hex | ios: : uppercase); //设置数制字符、十六进制、字母大写
cout<<”Hex:”<<num<<’\t”;
cout.setf(cout.flags() & ~ios:: hex) | ios: : oct); //取消十六进制、设置八进制输出
cout<<”Oec:”<<num<<endl;;
}
例: 使用函数设置标志字的例子
void main()
{cout<<”input integer”;
int num;
cin>>num;
cout<<”Dec:”<<num<<’\t’; //默认十进制
cout.setf(0x02c0); //设置数制字符、十六进制、字母大写,使用常量和设置标志字
cout<<”Hex:”<<num<<’\t”;
cout.unsetf(ios:: hex) ; //取消十六进制
cout.setf(ios:: Oct); //设置八进制输出
cout<<”Oec:”<<num<<endl;;
}
说明:1. ios::showbase | io::hex | ios: : uppercase
0080 0040 0200 和值为 : 0x02c0
2. cout.setf(cout.flags() & ~ios:: hex) | ios: : oct); //取消十六进制、设置八进制输出
13.2.3.3 设置域宽与填充方式
1. 设置域宽的两个重载函数
int ios::width( )
int ios::width(int)
2. 设置填充方式的两个重载函数
int ios::fill()
int ios::fill(int)
例:使用设置域宽的例子
#include “iostream.h”
main()
{ int num=-23,len;
cout<”12345678901234567890\n”;
len=cout.width();
cout<<num<<’\t’<<len<<endl;
cout.width(10);
len=cout.width();
cout.fill(‘#’);
cout<<num<<’\t’<<len<<endl;
cout.setf(ios:: left);
cout.width(len);
cout<<num<<’\t’<<len<<endl;
cout.width(10);
cout.setf(ios:: internal);
cout<<num<<’\t\<<len<<endl;
}
输出结果:
12345678901234567890
-23 0
#######-23 10
-23####### 10
-#######23 10
13.2.3.4 实数输出格式控制
int ios:: precision();
int ios:: precision(int);
13.2.3.5. i/o 操纵符
setbase(int)
endl
setfill(int)
setw(int)
例:使用操纵符的例子
#include “iostream.h”
#include “iomanip.h”
main()
{ int num=-23,len;
cout<”12345678901234567890\n”;
len=cout.width();
cout<<num<<’\t’<<len<<endl;
cout<<setw(20)<<num<<num<<endl; // setw( )每次只控制一个数据输出
cout<<setw(len)<<num<<num<<endl;
cout<<setw(10)<<num<<num<<endl; }
4
第 页 共 4 页
展开阅读全文