C++输入输出流--<iostream>详解
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>详解的更多相关文章
- Python基本语法_输入/输出语句详解
目录 目录 前言 输入 raw_input input raw_input 和 input 的区别 输出 print print 基本格式化输出 print复杂格式化输出 flags标志位 width ...
- Android系统输入事件分发详解
什么是输入事件? 我们知道,运行android系统的设备本质上是一台计算机,使用者在和计算机进行交互的时候可以抽象成简单的对计算机的输入和输出(IO).那么对于运行在计算机上的操作系统来说,操作系统在 ...
- 12.Linux之输入子系统分析(详解)
版权声明:本文为博主原创文章,转载请标注出处: 在此节之前,我们学的都是简单的字符驱动,涉及的内容有字符驱动的框架.自动创建设备节点.linux中断.poll机制.异步通知.同步互斥/非阻塞.定时 ...
- Android输入控件详解
输入控件 输入控件是您的应用用户界面中的交互式组件.Android 提供了多种可在 UI 中使用的控件,如按钮.文本字段.定位栏.复选框.缩放按钮.切换按钮等. 向 UI 中添加输入控件与向 XML ...
- logstash中关于Jdbc输入配置选项详解
Setting Input type Required clean_run boolean No columns_charset hash No connection_retry_attempts n ...
- android EditText输入变化事件详解
editText.addTextChangedListener(new TextWatcher(){ public void afterTextChanged(Editable s) { // ...
- 【转】scp命令详解
先说下常用的情况: 两台机器IP分别为:A.104.238.161.75,B.43.224.34.73. 在A服务器上操作,将B服务器上/home/lk/目录下所有的文件全部复制到本地的/root目录 ...
- Java I/O输入输出流详解
一.文件的编码 开发时一定要注意项目默认的编码!!!!!!!! 文件操作的时候一定要记得关闭!!!!!!!! ASCII:美国标准 ...
- Java-IO 输入输出流详解
一.文件的编码 开发时一定要注意项目默认的编码!!!!!!!! 文件操作的时候一定要记得关闭!!!!!!!! ASCII:美国标准 ...
随机推荐
- gitlab的简单使用
mkdir folder //创建文件夹 cd folder/ git clone git连接 git status //git当前的状态 ls //当前目录下的所有文件 cd gitFolder 进 ...
- PDF转HTML的方法。
上个项目客户提出了一个需求,要求把PDF格式的文件转化为HTML格式. 上网查了一下,要么使用软件处理,要么是HTML格式转化为PDF.因为涉及到图文识别问题,所以说仅仅依靠前端不能实现.在网上查了几 ...
- mysql随机取出若干条记录的实用方法
1.常见的方法 ; 这种方法可以随机取得数据,但是如果表比较大,数据量很多的时候会很耗时. 2.优化后的方式 ) as t ); 分析,首先根据条件筛选出要选的数据,然后随机排序取出要的条数的id , ...
- Matplotlib的初次使用
# -*- coding: utf-8 -*-#先画一个线性图 import numpy as np import matplotlib.pyplot as plt x=[0,1] y=[0,1] p ...
- [BZOJ 5323][Jxoi2018]游戏
传送门 \(\color{green}{solution}\) 它每次感染的人是它的倍数,那么我们只需要找出那些除了自己以外在\(l\), \(r\)内没有别的数是 它的约数的数,在这里称其为关键数. ...
- thinkphp5.0 微信扫码支付模式二
仅供个人参考,方便大家. 一.1)https://pay.weixin.qq.com/index.php/core/home/login 复制此地址 打开微信商户平台. 2)下载安全操作证书(最好在 ...
- POJ-1258 Agri-Net(最小生成树)
Description Farmer John has been elected mayor of his town! One of his campaign promises was to brin ...
- Mac下快速新建txt文件
1.打开终端,定位到桌面 cd desktop 2.输入 vi test.txt 此时,一个txt文件就会建立在桌面上,操作vi时的提示:按[i]为输入内容,编辑好之后按[esc]键,然后输入[:wq ...
- Docker MySQL基本操作
1 启动mysql实例 docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:t ...
- 实现基于NTP协议的网络校时功能
无论PC端还是移动端系统都自带时间同步功能,基于的都是NTP协议,这里使用C#来实现基于NTP协议的网络校时功能(也就是实现时间同步). 1.NTP原理 NTP[Network Time Protoc ...