C++输入输出流包含在头文件<iostream>中,

流的定义如下:
通过设备驱动程序与键盘、屏幕、文件、打印机等进行交互, iostream 类提供与之交互的方法。
输出流:
输出流的对象是字节目标,三个重要的输出流类是ostream、ofstream和ostringsream。
Ostream派生于basic_ostream支持预定义的流对象又:
cout标准输出
cerr标准错误输出,不经过缓冲
clog类似cerr,使用缓冲
注:缓冲是指将所有输出集中存放,然后一次性显示在屏幕上,避免多次刷屏。

格式控制
输出宽度:
输出宽度可以采用<iostream>中自带的width()函数,或者使用< iomanip >中的setw, setw 和宽度均不截断值。

使用width()函数代码如下:

 #include "stdafx.h"
#include <iostream>
using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
double values[] = { 1.23, 35.36, 653.7, 4358.24 };
for (int i = ; i < ; i++)
{
cout.width();
cout << values[i] << '\n';
}
getchar();
return ;
}

使用setw()函数

 #include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
double values[] = { 1.23, 35.36, 653.7, 4358.24 };
for (int i = ; i < ; i++)
{
//cout.width(10);
cout << setw() << values[i] << '\n';
}
getchar();
return ;
}

程序运行结果:

宽度设置

设置宽度后,cout默认空白填充,如果需要填充某个字符,可采用fill()或setfill()函数。
采用fill()函数

 #include "stdafx.h"
#include <iostream>
using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
double values[] = { 1.23, 35.36, 653.7, 4358.24 };
for (int i = ; i < ; i++)
{
cout.width();
cout.fill('*');
cout << values[i] << '\n';
}
getchar();
return ;
}

采用setfill()函数

 #include "stdafx.h"
#include <iomanip>
#include <iostream>
using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
double values[] = { 1.23, 35.36, 653.7, 4358.24 };
for (int i = ; i < ; i++)
{
cout.width(); cout << setfill('*') << values[i] << '\n';
}
getchar();
return ;
}

程序运行结果:

精度设置

浮点的默认精度默认为六,如果需要修改,使用setprecision()。数字输出可以设置为固定型和科学型,输出形式采用setiosflags(ios::fixed)控制,fixed表示固定型,scientific表示科学型,默认为科学型。

科学型代码:

 #include "stdafx.h"

 #include <iostream>
using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
double values[] = { 1.23, 35.36, 653.7, 4358.24 };
for (int i = ; i < ; i++)
cout << setprecision()
<< values[i]
<< endl;
getchar();
return ;
}

运行结果:

使用固定记数法

 

 #include "stdafx.h"
#include <iomanip>
#include <iostream>
using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
double values[] = { 1.23, 35.36, 653.7, 4358.24 };
for (int i = ; i < ; i++)
cout << setiosflags(ios::fixed) << setprecision()
<< values[i]
<< endl;
getchar();
return ;
}

运行结果:

将整形数字按照不同进制输出:

 #include "stdafx.h"

 #include <iostream>
using namespace std; int _tmain(int argc, _TCHAR* argv[])
{ cout << << endl;//十进制
cout <<dec<< << endl;//十进制
cout << oct << << endl;//八进制
cout << hex << << endl;//十六进制 getchar();
return ;
}

运行结果:

输入输出数据到文件:

 #include "stdafx.h"
#include <iostream>
#include <fstream> using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
ifstream ifile;
char buff[] = { };
ifile.open("d:/FILE1.txt", ios::in);
ifile.getline(buff, );
// Do some output
ifile.close(); // FILE1 closed cout << buff << endl;// getchar();
return ;
}

运行结果:

采用>>运算符读入整个字符串:

 #include "stdafx.h"
#include <iostream>
#include <fstream> using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
ifstream ifile;
char buff[] = { };
ifile.open("d:/FILE1.txt", ios::in);
ifile >> buff;
// Do some output
ifile.close(); // FILE1 closed cout << buff << endl;// getchar();
return ;
}

运行结果:

写入文件主要采用以下函数:

cout.flush()      //刷新缓冲区

cout.put()        //把字符写入流中

cout.write()      //将字符串写入当前输出流中

代码如下:

 #include "stdafx.h"
#include <iostream>
#include <fstream> using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
ofstream ofile;
char buff[] = { };
ofile.open("d:/FILE1.txt", ios::in);
if (!ofile)
{
cout << "打开文件失败" << endl;
}
ofile << "" << endl;
ofile.write("xyz", );
ofile.put('M');
ofile.flush();//清空缓冲区
ofile.close(); // FILE1 closed getchar();
return ;
}

运行结果:

C++输入输出流--<iostream>详解的更多相关文章

  1. Python基本语法_输入/输出语句详解

    目录 目录 前言 输入 raw_input input raw_input 和 input 的区别 输出 print print 基本格式化输出 print复杂格式化输出 flags标志位 width ...

  2. Android系统输入事件分发详解

    什么是输入事件? 我们知道,运行android系统的设备本质上是一台计算机,使用者在和计算机进行交互的时候可以抽象成简单的对计算机的输入和输出(IO).那么对于运行在计算机上的操作系统来说,操作系统在 ...

  3. 12.Linux之输入子系统分析(详解)

    版权声明:本文为博主原创文章,转载请标注出处:   在此节之前,我们学的都是简单的字符驱动,涉及的内容有字符驱动的框架.自动创建设备节点.linux中断.poll机制.异步通知.同步互斥/非阻塞.定时 ...

  4. Android输入控件详解

    输入控件 输入控件是您的应用用户界面中的交互式组件.Android 提供了多种可在 UI 中使用的控件,如按钮.文本字段.定位栏.复选框.缩放按钮.切换按钮等. 向 UI 中添加输入控件与向 XML ...

  5. logstash中关于Jdbc输入配置选项详解

    Setting Input type Required clean_run boolean No columns_charset hash No connection_retry_attempts n ...

  6. android EditText输入变化事件详解

    editText.addTextChangedListener(new TextWatcher(){ public void afterTextChanged(Editable s) {    // ...

  7. 【转】scp命令详解

    先说下常用的情况: 两台机器IP分别为:A.104.238.161.75,B.43.224.34.73. 在A服务器上操作,将B服务器上/home/lk/目录下所有的文件全部复制到本地的/root目录 ...

  8. Java I/O输入输出流详解

    一.文件的编码               开发时一定要注意项目默认的编码!!!!!!!!               文件操作的时候一定要记得关闭!!!!!!!!        ASCII:美国标准 ...

  9. Java-IO 输入输出流详解

    一.文件的编码               开发时一定要注意项目默认的编码!!!!!!!!               文件操作的时候一定要记得关闭!!!!!!!!        ASCII:美国标准 ...

随机推荐

  1. spring管理hibernate session的问题探究

    我们再用spring管理hibernate的时候, 我们会继承HibernateDaoSupport 或者HibernateTemplate类. 我们不知道这两个类之间有什么关系. 也没有去关闭ses ...

  2. 【javascrpt】——图片预览和上传,兼容IE 9-

    下载DEMO:https://github.com/CaptainLiao/zujian/tree/master/Upload 对于现代浏览器来说,要实现图片预览非常简单: 1.fileReader. ...

  3. springboot集成JsonRpc2.0

    导入依赖的jar: 配置AutoJsonRpcServiceImplExporter: 接口文件: 实现类: 测试:

  4. SSM搭建

    SSM搭建 SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架.. Sprin ...

  5. 架构师养成记--22.客户端与服务器端保持连接的解决方案,netty的ReadTimeoutHandler

    概述 保持客户端与服务器端连接的方案常用的有3种 1.长连接,也就是客户端与服务器端一直保持连接,适用于客户端比较少的情况. 2.定时段连接,比如在某一天的凌晨建立连接,适用于对实时性要求不高的情况. ...

  6. java的MethodHandle类详解

    一.总述   java7为间接调用方法提供了MethodHandle类,即方法句柄.可以将其看作是反射的另一种方式. 这是使用MethodHandle调用方法的一个例子: public class T ...

  7. abp angular 前端权限控制

    import { AppComponentBase } from '@shared/app-component-base'; this.permission.isGranted(menuItem.pe ...

  8. 一、OPENERP 的一个demo

    安装好OPENERP后,使用 ps -aux|grep openerp 从输出的内容可以得到OPENERP的安装信息, /usr/bin/python /usr/bin/openerp-server ...

  9. Windows下的VMware导入到Mac的VMware Function

    在windows下是以文件夹的形式存在的,但是在Mac下是以.vmwarevm为后缀的文件. 操作步骤: 把windows下的虚拟机整个文件夹拷贝到Mac,然后文件夹后面加上.vmwarevm. 然后 ...

  10. (转)Db2 备份恢复性能问题诊断与调优

    原文:https://www.ibm.com/developerworks/cn/analytics/library/ba-lo-backup-restore-performance-issue-ju ...