C++ 格式化输出 及 输入 流


#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;

返回浮点数输出精度,即输出的有效数字的位数


文件流类:

文件流类包括:文件输入流类ifstream、文件输出流类ofstream和文件输入/输出流类fstream。相应地,在打开文件之间,要先创建ifstream、ofstream或fstream类的流对象。 

 

定义后打开: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));

qC++使用的是类型安全的I/O操作,可以对数据流进行错误检测,用户可根据标志位来判断操作I/O的成功与失败。程序员在try块中放上出错时产生异常的代码。try块后面是一个或几个catch块。每个catch块指定捕获和处理一种异常,而且每个catch块包含一个异常处理器(异常处理程序)。

标准输出函数cout :

/*关于浮点数的格式*/
#include <iostream.h>
void main()
{
    float f=2.0/3.0,f1=0.000000001,f2=-9.9;
    cout<<f<<' '<<f1<<' '<<f2<<endl;      //正常输出
    cout.setf(ios::showpos);              //强制在正数前加+号
    cout<<f<<' '<<f1<<' '<<f2<<endl;
    cout.unsetf(ios::showpos);            //取消正数前加+号
    cout.setf(ios::showpoint);            //强制显示小数点后的无效0
    cout<<f<<' '<<f1<<' '<<f2<<endl;
    cout.unsetf(ios::showpoint);          //取消显示小数点后的无效0
    cout.setf(ios::scientific);           //科学记数法
    cout<<f<<' '<<f1<<' '<<f2<<endl;  
    cout.unsetf(ios::scientific);         //取消科学记数法
    cout.setf(ios::fixed);                //按点输出显示
    cout<<f<<' '<<f1<<' '<<f2<<endl;
    cout.unsetf(ios::fixed);              //取消按点输出显示
    cout.precision(18);                   //精度为18,正常为6
    cout<<f<<' '<<f1<<' '<<f2<<endl;
    cout.precision(6);                    //精度恢复为6


操纵算子:
/*关于浮点数的格式*/
#include <iomanip.h>
void main()
{
    float f=2.0/3.0,f1=0.000000001,f2=-9.9;
    cout<<f<<' '<<f1<<' '<<f2<<endl;      //正常输出
    cout<<setiosflags(ios::showpos);      //强制在正数前加+号
    cout<<f<<' '<<f1<<' '<<f2<<endl;
    cout<<resetiosflags(ios::showpos);    //取消正数前加+号
    cout<<setiosflags(ios::showpoint);    //强制显示小数点后的无效0
    cout<<f<<' '<<f1<<' '<<f2<<endl;
    cout<<resetiosflags(ios::showpoint);  //取消显示小数点后的无效0
    cout<<setiosflags(ios::scientific);   //科学记数法
    cout<<f<<' '<<f1<<' '<<f2<<endl;  
    cout<<resetiosflags(ios::scientific); //取消科学记数法
    cout<<setiosflags(ios::fixed);        //按点输出显示
    cout<<f<<' '<<f1<<' '<<f2<<endl;
    cout<<resetiosflags(ios::fixed);       //取消按点输出显示
    cout<<setprecision(18);               //精度为18,正常为6
    cout<<f<<' '<<f1<<' '<<f2<<endl;
    cout<<setprecision(6);                //精度恢复为6
}

1.打印浮点数控制位数

Java代码  
setf: set flag   
  
#incluce <iomanip>// 操作元manipulator   
cout.setf(ios::fixed);   
cout.setf(ios::showpoint);   
cout.precision(2); //用两位小数显示   
cout<< your_double_variable<<endl;   
cout.precision(5); //再次改变时只需加这一句就可以了。    
cout::setf(ios::showpos);//正数显示时加正号   
cout::setf(ios::scientific);//用科学计数法表示   
cout::setf(ios::left);//左对齐   
cout::setf(ios::right);//右对齐   
cout.width(4);//只对一次有用   
cout<<setw(4)<<4<<endl;//控制输出宽度   
cout.unsetf(ios::showpos);//取消设置标志位 

setf: set flag

#incluce <iomanip>// 操作元manipulator
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2); //用两位小数显示
cout<< your_double_variable<<endl;
cout.precision(5); //再次改变时只需加这一句就可以了。 
cout::setf(ios::showpos);//正数显示时加正号
cout::setf(ios::scientific);//用科学计数法表示
cout::setf(ios::left);//左对齐
cout::setf(ios::right);//右对齐
cout.width(4);//只对一次有用
cout<<setw(4)<<4<<endl;//控制输出宽度
cout.unsetf(ios::showpos);//取消设置标志位
  

来源:http://www.cnblogs.com/bamboo-talking/archive/2011/03/20/1989775.html

http://www.cnblogs.com/netact/archive/2012/02/08/2335640.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值