输入:
读入:a+b,a-b,a/b等,cin>>a>>charOperation>>b>>endl;
enum {
skipws = 0x0001, //跳过当天及后面所有连续的空白符。
left = 0x0002,
right = 0x0004,
internal = 0x0008,//在指定的域宽内数值的符号按左对齐、数值本身按右对齐输出。
dec = 0x0010, //10
oct = 0x0020, //8
hex = 0x0040, //16
showbase= 0x0080, //8进制:0、16进制:0x ,10无。
showpoint= 0x0100,
uppercase= 0x0200,
showpos= 0x0400,
scientific= 0x0800,
fixed = 0x1000,
unitbuf = 0x2000,
stdio = 0x4000
};
//x3_1.cpp,指定格式的输入输出流的各种控制符的使用
#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
int x=1000;
double y=1.23456789;
cout<<"默认x值:"<<x<<endl;
cout<<"十进制:"<<dec<<x<<endl; //dec oct hex 会一直作用。
cout<<"八进制:"<<oct<<x<<endl;
cout<<"十六进制:"<<hex<<x<<endl;
cout<<"十六进制(大写字母):"<<hex<<uppercase<<x<<endl<<endl;
cout<<"默认y值(左对齐且有效数字位数为6):"<<y<<endl;
cout<<"宽度为10并右对齐:"<<setw(10)<<right<<y<<endl;
cout<<"宽度为8:"<<setw(8)<<y<<endl;
cout<<"宽度为4:"<<setw(4)<<y<<endl;
cout<<"用*号填充空位(10位宽度):"<<setfill('*')<<setw(10)<<y<<endl; //setw(int i) 只对紧随的数据显示有影响。控制多个数据要多个setw()method.
cout<<"设精度为3输出y(不包括小数点):"<<setprecision(3)<<y<<endl; //setprecision(int i) 会一直作用
cout<<"设精度为8输出y(不包括小数点):"<<setprecision(8)<<y<<endl;
cout<<"显示正负号:"<<showpos<<y<<endl;
cout<<"用科学计数法表示y:"<<scientific<<y<<endl;
cout<<"用科学计数法表示y(控制E前数据的小数点后位数):"<<scientific
<<setprecision(3)<<y<<endl;
}
成 员 函 数 |
含 义 |
long setf(long _f); |
根据参数_f设置相应的格式化标志,返回当前的设置 |
long setf(long _l,long _f); |
取消_l的设置,根据_f设置格式化标志,返回当前的设置 |
long unsetf(long _f); |
根据参数_f取消相应的格式化标志,返回当前的设置 |
long flags(long _f); |
根据参数_f重新设置相应的格式化标志,返回当前的设置 |
long flags() const; |
返回当前用于I/O流控制的格式化标志设置 |
int width(int w); |
设置下一个数值的输出域宽为w,返回为输出上一个数值所设置的域宽 |
int width() const; |
返回当前的输出域宽 |
char fill(char c); |
设置流中用于输出数据的填充字符为c,返回此前设置的填充字符。系统预设置的填充字符为空格 |
char fill() const; |
返回当前使用的填充字符 |
int precision(int n); |
设置浮点数的输出精度为n,返回此前设置的输出精度。系统预设置的输出精度为6 |
int precision() const; |
返回浮点数输出精度,即输出的有效数字的位数 |
文件流类:
定义后打开:void open(const char *filename, int mode,int prot=filebuf::openprot);
定义时打开:文件流类名 文件流对象名(const char *filename, int mode, int prot=filebuf::openprot);
枚 举 常 量 |
含 义 |
ios::in |
打开文件进行读操作 |
ios::out |
打开文件进行写操作 |
ios::ate |
打开文件时文件指针定位到文件尾 |
ios::app |
添加模式,所有增加都在文件尾部进行 |
ios::trunc |
如果文件已存在则清空原文件 |
ios::binary |
二进制文件(非文本文件) |
在文件打开操作函数的参数里,文件路径分隔符必须写成'\\'。如果是多层路径,每个隔符都必须使用双反斜线。
(3)prot:文件的访问方式,取值为:
0:普通文件
1:只读文件
2:隐含文件
3:系统文件
默认值为0,表示对一个普通文件的访问。
字符文件写入、读出文件。
写入到文件:插入运算符“<<”。调用类中的成员函数put()。
读出到内存:提取运算符“>>”。 调用类中的成员函数get()或get(char&)。成员函数getline(char* buffer, int len, char='\n')。
二进制写入 读出文件:
ofstreamoutput("student.dat",ios::binary); output.write((char *)&s,sizeof(s));
ifstreaminput("student.dat",ios::binary); input.read((char *)&s,sizeof(s));