C++ Input & Output
1.C++ I/O各类之间的继承关系图

参考网址:
http://www.cplusplus.com/reference/iolibrary/
Note:
在程序中包含iostream文件将自动创建8个流对象(4个用于窄字符流, 4个用于宽字符流),如下:
cin --标准输入流,该流被关联到标准输入设备(通常为键盘) ----> wcin
cout --标准输入流,该流被关联到标准输出设备(通常为显示器) ----> wcout
cerr --标准错误流,类似cout,但cerr流没有缓冲区 ----> wcerr
clog --标准错误流,类似cerr,但clog流拥有缓冲区 ----> wlog
由上图可知,cin 是一个istream对象,拥有istream的相关成员函数; cout是一个ostream对象,拥有ostream的相关成员函数和数据成员
2.I/O 常见的成员函数
对于istream类而言,其常见的成员函数如下(以cin对象实例):
istream::get
--istream对get方法进行了重载,常见的调用get的形式有4种,如下:
1) int get(); //读取下一个字符,返回该字符的ascii码
2) istream & get(char & c); //读取下一个字符,并将其赋值给字符变量 c
3) istream & get(char * s, streamsize n); //从输入流中读取指定大小的字节(参数n),并存储到s所指向的地址空间中
4) istream & get(char * s, streamsize n, char delim); //冲输入缓冲区中读取n字节,并存储到s指向的存储空间中
Note:
1) get的第3种形式和第4种形式,均读区最多n字节的内容;不同之处在于
istream & get(char * s, streamsize n);在读取n字节或遇到换行符时,停止读取
istream & get(char * s, streamsize n, char delim);在读取n字节或遇到delim指定的分隔符时停止读取
形式3相当于如下的形式4样式:
get(char * s, streamsize n) ----> get(char * s, streamsize n, '\n');
2) get的后两种形式中,当读取的字符超过n指定的值时,get不会重置流状态
istream::getline
istream & getline(char * s, streamsize n);
istream & getline(char * s, streamsize n, char delim);
Note:
getline 与 get都可以用来读取一行输入,不同在于
1) getline读取一行时,连同换行符'\n'一起从输入流中读取,然后丢弃\n,在读取的字符最后加上\0空字符,以表示字符串结尾
get不会读取\n换行符,\n依然保留在输入流中,以备后续的读取
2) getline读取超过n个字节时,重置流状态,设置failbit等流标志位;get函数则不会进行该操作
istream::ignore
istream & ignore(streamsize n, char delim = EOF); //提取并丢弃字符
istream::gcout
streamsize gcount() const; //获取最后一次非格式化读取的字节数
istream::peek
int peek(); //查看输入流中下一个字符,返回该字符的ascii码
Note: peek仅仅查看,并不从输入流中读取字符
istream::putback
istream & putback(char c); //将字符c放回输入流中(插入到输入流中,以备下一次读取)
istream::read
istream & read(char * s, streamsize n); //从输入流中读取n字节,并存储到s指定的存储空间中
其他:tellg , seekg //用于输入流定位的函数
对于ostream类而言,其常见的成员函数如下(以cout对象实例):
ostream::put
ostream & put(char c); //将字符c插入到输出流中,即打印字符c
ostream::write
ostream & write(const char * s, streamsize n); //将s从的前n个字节插入到输入流中
其他:
tellp, seekp //用于输出流定位的函数
flush //刷新输出流
3.文件操作
ifstream::open
void open(const char * filename, ios_base::openmode mode = ios_base::in); //打开filename文件,并将它与ifstream对象关联
void open(const string & filename, ios_base::openmode mode = ios_base::in);
ifstream::is_open
bool is_open() const; //检测文件是否与ifstream对象关联
ifstream::close
void close(); //关闭ifstream对象与文件的链接
ofstream::open
void open(const char * filename, ios_base::openmode mode = ios_base::out);
void open(const string & filename, ios_base:openmode mode = ios_base::out);
其他:ofstream::is_open, ofstream::close与ifstream相同
Note:
1) ios_base::openmode指定了打开文件的模式,常见的模式有:
ios_base::app //append
ios_base::ate //at end
ios_base::in //input
ios_base::out //output
ios_base::trunc //truncate
ios_base::binary //binary
2)可将多个openmode通过 | (与运算)进行组合,如:
ios_base::out|ios_base::trunc
C++ Input & Output的更多相关文章
- PHP-FPM-failed to ptrace(PEEKDATA) pid 123: Input/output error
If you're running PHP-FPM you can see these kind of errors in your PHP-FPM logs. $ tail -f php-fpm.l ...
- NFS挂载异常 mount.nfs: Input/output error
[root@localhost ~]# vi /etc/exports #增加/nfs 192.168.10.132(rw,no_root_squash,no_all_squash,async) [r ...
- BIOS(Basic Input/Output System)是基本输入输出系统的简称
BIOS(Basic Input/Output System)是基本输入输出系统的简称 介绍 操作系统老师说,平时面试学生或者毕业答辩的时候他都会问这个问题,可见这个问题对于计算机专业的学生来说是如此 ...
- read()、write()返回 Input/output error, Device or resource busy解决
遇到的问题,通过I2C总线读.写(read.write)fs8816加密芯片,报错如下: read str failed,error= Input/output error! write str fa ...
- Angular 个人深究(三)【由Input&Output引起的】
Angular 个人深究(三)[由Input&Output引起的] 注:最近项目在做别的事情,angular学习停滞了 1.Angular 中 @Input与@Output的使用 //test ...
- Docker 在转发端口时的这个错误Error starting userland proxy: mkdir /port/tcp:0.0.0.0:3306:tcp:172.17.0.2:3306: input/output error.
from:https://www.v2ex.com/amp/t/463719 系统环境是 Windows 10 Pro,Docker 版本 18.03.1-ce,电脑开机之后第一次运行 docker ...
- dpdk EAL: Error reading from file descriptor 23: Input/output error
执行test程序时输出: EAL: Error reading from file descriptor 23: Input/output error 原因: 在虚拟机添加的网卡,dpdk不支持导致的 ...
- html5 填表 表单 input output 与表单验证
1.<output> Js计算结果 <form oninput="res.value = num1.valueAsNumber*num2.valueAsNumber ...
- mount_cd9660:/dev/acd0: Input/output error
mount -t cd9660 /dev/acd0 /cdrom g_vfs_done():acd0[READ(offset32768, length=204]error =5 mount_cd966 ...
- Input/output subsystem having an integrated advanced programmable interrupt controller for use in a personal computer
A computer system is described having one or more host processors, a host chipset and an input/outpu ...
随机推荐
- Linux下安装memcached
Linux下安装memcached 1.运行memcached需要本文开头介绍的libevent库 $ sudo yum install libevent libevent-deve 2.下载安装me ...
- CCNA实验(3) -- RIP
RIP协议分为版本1和版本2,均具备以下特征:1.是距离向量路由协议2.使用跳数(Hop Count)作为度量值3.默认路由更新周期为30秒4.管理距离(AD)为1205.支持触发更新6.最大跳数为1 ...
- jquery阻止默认滑动
$(".swiper-slide").click(function(){ var index = imgarr[$(this).index()]; var content = &q ...
- 使用Xib添加自定义View
1.新建Cocoa Touch Class以及UI View,2者同名 2.设置UI View的File's Owner——Custom Class为之前新建类 3.设置Xib中View与类关联 4. ...
- List(双向链表)
List是一种双向链表结构,可以从第一个元素开始删除.插入,也可以从最后一个元素删除.插入,下面介绍一下 List 中常用的几个函数: 一.List 中的 begin 和 end 函数 : 和其他几种 ...
- Lucence.Net学习+盘古分词
创建索引库 //读取文件,存储到索引库 public string CreateDatebase() { //获取索引库的路径 ...
- UNIX/Linux-进程控制(实例入门篇)
UNIX进程 进程标识符 要想对进程控制,必须得获取进程的标识.每个进程都有一个非负整数表示的唯一进程ID,虽然是唯一的,但是进程ID可以重用.当一个进程终止后,其进程ID就可以再次使用了. 系统 ...
- thinkphp phpexcel导入
上次做了一个基于tp3.2.3的phpexcel导出,这次是phpexcel导入,准备材料phpexcel(不知道下载地址的查看我上一篇博文),虽说是基于thinkphp3.2.3来的,也只不过是引入 ...
- C单链表实现
/* * LinkNode.c * * Created on: Jan 14, 2014 * Author: root */ #include <stdlib.h> #include &l ...
- 使用 dotnet watch 开发 ASP.NET Core 应用程序
使用 dotnet watch 开发 ASP.NET Core 应用程序 原文:Developing ASP.NET Core applications using dotnet watch作者:Vi ...