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. leetcode-200-岛屿的个数(dfs找所有的连通分量)

    题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...

  2. 2016级算法期末上机-H.难题·AlvinZH's Fight with DDLs III

    1119 AlvinZH's Fight with DDLs III 思路 难题,最小点覆盖. 分析题意,某一个任务,既可以在笔记本A的 \(a\) 模式下完成,也可以在笔记本B的 \(b\) 模式下 ...

  3. 安装配置python、beautifulsoup4、pip的心酸总结

    1.python下载安装不纠结,但如果要加入到eclipse里面就要注意一下版本,版本不匹配会造成,要不python降级,要不eclipse升级的情况 2.在稍新版本的python立面就附带下载在了p ...

  4. 关于nginx的一个错误操作记录

    今天在弄前后端同步的测试的时候,前端用Nginx代理访问后端接口,由于启动了多次nginx软件,没有将前几次启动的nginx进程关闭,导致在访问后端接口的request被挂起,过了半天也没有结果返回, ...

  5. 网络基础 02_TCP/IP模型

    1 TCP/IP参考模型概述   2 应用层 3 传输层 3.1 传输控制协议(TCP) 面向连接 可靠传输 流控及窗口机制 使用TCP的应用: Web浏览器:电子邮件: 文件传输程序 3.2 用户数 ...

  6. 移动端优化 && 清除移动端网站点击a标签时闪现的边框或遮罩层(CSS) && 移动端点击 && 文字不可选择

      在移动端网站,当你点击加了a标签的文字或图片时,该元素的周围会闪现一个蓝色的边框,在微信上的网站就是如此:而有的浏览器会闪现一个半透明遮罩层,比如移动端的Chrome浏览器,其实这些特效无非就是为 ...

  7. Linus' Law

    Given enough eyeballs, all bugs are shallow.                                               ------埃里克 ...

  8. Google发布移动网站设计原则

    Google 刚刚发布了由 Google 与 AnswerLab 联合打造,名为<Principles of Mobile Site Design: Delight Users and Driv ...

  9. 如何修改 linux ubuntu 默认语言

    最近学习linux中,由于安装时选择了简体中文作为默认语言,时常出现乱码现象,所以决定将默认语言改回en_US. 但是在网上找了一些教程,发觉不一定都能实现.现总结如下: (1)对于大部分linux系 ...

  10. h2数据库的简单使用

    1.登录H2数据库的WebConsole控制台 2.设置数据库连接 3.连接测试通过之后,点击[连接]按钮,登录到test数据库的webConsole 4.创建表 复制H2数据库提供的样例SQL脚本, ...