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 ...
随机推荐
- localstroge可以在页面间传递数值;
连接地址为:http://4.suancai.sinaapp.com/localstorg/a.html 原理是,a页面设置了sessionstorge,b页面可以访问到; 并且已关闭浏览器,sest ...
- sql的强大功能(看一条sql解决的复杂业务)
一条sql语句解决的复杂业务,请往下看: 业务介绍:一个单位有多个立项(立项信息表里有单位id),每个立项可能被预警多次(预警信息表里的uuid字段的值里包含有立项id或单位id),每 ...
- telnet登陆路由器。。。
登陆路由有两种方式.一种是console,还有一种是使用telnet,由于我电脑是win7,不支持console.也懒得装软件,就使用telnent为例. 一.开启telnetclient 对于XP. ...
- .NET + Jcrop 实现在线裁图功能
最近有这样一个需求,因为一个门户网站首页展示图片很长但很矮,自己截图怕有不到位,所以利用JQUERY 的 Jcrop组件做了一个在线裁图的功能. 初始化 $('#oldpicImg').Jcrop({ ...
- 2014年辛星Javascript解读第二节
本小节我们解说一下Javascript的语法,尽管js语言很easy,它的语法也相对好学一些,可是不学总之还是不会的,因此,我们来一探到底把. ********凝视************* 1.我们 ...
- linux中时间函数
linux下常用时间类型有四种: time_t . struct tm. struct timeval . struct timespec 1.time_t 时间函数 time_t ...
- activity的横屏和竖屏设置
主要在清单文件这样配置: <application android:allowBackup="true" android:icon="@drawable/ic_la ...
- C++对象模型4--有重写的单继承
有重写的单继承 派生类中重写了基类的print()函数. //Derived_Overwrite.h #pragma once #include "base.h" class De ...
- CRM中的一个函数,保存一下,别系统被ぅ崩坏就麻烦了.
CREATE OR REPLACE function UXQLCRM.GET_WEI_XIU(htfid in varchar2) ); CURSOR cr_bg_jl is select " ...
- jQuery.fn.serialize 阅读
今天第一次阅读jQuery源码,因为读到用js对表单的序列化,为的是在ajax操作中将表单中各个域的值传到服务器.书上用了很长的步骤,判断每一个表单域的属性,然后拼接. 大概是这样: function ...