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:美国标准 ...
随机推荐
- SpringMvc渲染视图
这篇博文讨论的问题是从ModelAndView如何渲染到页面. 首先要知道每个请求处理完之后都会返回一个ModelAndView对象. 这里我分6种情况来分析,代表6种返回类型: ModelAndVi ...
- 使用 Hexo + github 搭建个人博客
来自:http://www.cnblogs.com/fengzheng/p/8031518.html Hexo 是什么 Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或 ...
- jquery json实现面向对象 百度十二星座
效果: 源码: index.html <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- Machine learning第四周code 编程作业
1.lrCostFunction: 和第三周的那个一样的: function [J, grad] = lrCostFunction(theta, X, y, lambda) %LRCOSTFUNCTI ...
- 完美解决Bootstrap4 导航栏 fixed-top 后,锚点定位时遮挡问题
利用锚点改变事件\(onhashchange\),使用jQuery的\(scrollTop\)向前滚回导航栏的高度(比如我的100个像素) HTML: <body onhashchange=&q ...
- Mac下一台电脑管理多个SSH KEY(转)
一.关于ssh是什么? http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html 二.需求: 一台电脑上(Mac os)管理多个ssh ...
- shiro学习笔记_0500_授权
1,授权:给身份认证通过的人,授予他可以访问某些资源的权限. 2,权限粒度:分为粗粒度和细粒度. 粗粒度:例如对 user 的 crud,也就是通常所说的对表的操作. 细粒度:对表中记录的操作.如 只 ...
- 修改zend studio字体大小
第一步:进入设置窗口 windows -> preferences第二步:进入修改字体的选项卡. General -> Appearance -> Colors and ...
- EJB与JavaBean
JavaBean是一个组件,而EJB就是一个组建框架.JavaBean面向的是业务逻辑和表示层的显示,通过编写一个JavaBean,可以将业务逻辑的事件和事务都放在其中,然后通过它的变量属性将所需要的 ...
- android 签名验证防止重打包
网上资料很多,这里只做一个笔记反编译 dex 修改重新打包签名后 apk 的签名信息肯定会改变,所以可以在代码中判断签名信息是否被改变过,如果签名不一致就退出程序,以防止 apk 被重新打包. 1 j ...