原创作品,转载请注明出处:http://www.cnblogs.com/shrimp-can/p/5657192.html

一.成员类型

1. ios::fmtflags

格式标志,常用来设置输出的格式,用于函数flags、setf、unsetf作为其参数或返回类型。

field member constant effect when set
independent flags boolalpha read/write bool elements as alphabetic strings (true and false).
showbase write integral values preceded by their corresponding numeric base prefix.
showpoint write floating-point values including always the decimal point.
showpos write non-negative numerical values preceded by a plus sign (+).
skipws skip leading whitespaces on certain input operations.
unitbuf flush output after each inserting operation.
uppercase write uppercase letters replacing lowercase letters in certain insertion operations.
numerical base 
(basefield)
dec read/write integral values using decimal base format.
hex read/write integral values using hexadecimal base format.
oct read/write integral values using octal base format.
float format 
(floatfield)
fixed write floating point values in fixed-point notation.
scientific write floating-point values in scientific notation.
adjustment 
(adjustfield)
internal the output is padded to the field width by inserting fill characters at a specified internal point.
left the output is padded to the field width appending fill characters at the end.
right the output is padded to the field width by inserting fill characters at the beginning.

2. ios_base::iostate

flag value indicates
eofbit End-Of-File reached while performing an extracting operation on an input stream.
failbit The last input operation failed because of an error related to the internal logic of the operation itself.
badbit Error due to the failure of an input/output operation on the stream buffer.
goodbit No error. Represents the absence of all the above (the value zero).

3. 其他:sentry、event、event_callback、failure、Init、openmode、seekdir

二.从输入流类istream继承的函数

1. gcount:streamsize gcount() const;

返回从最后次非格式化输入操作中提取的读取的字符个数

  char str[20];
std::cout << "Please, enter a word: ";
std::cin.getline(str,20);
std::cout << std::cin.gcount() << " characters read: " << str << '\n';
输出为:
Please, enter a word: simplify
9 characters read: simplify

2. get:int get();读取一个字符,通常用来排除换行符的影响

istream& get (char& c);读取一个字符到c中

istream& get (char* s, streamsize n);读取一个字符串,直到遇到换行符或者读取了n-1个字符,会自动在末尾添加字符'\0'

istream& get (char* s, streamsize n, char delim);读取一个字符串,直到遇到字符delim或者读取了n-1个字符,会自动在末尾添加字符'\0'

换行符会留在输入队列中

3. getline:istream& getline (char* s, streamsize n );读取一个字符串,直到遇到换行符或者读取了n个字符(换行符也会读取),并将换行符替换为'\0'

istream& getline (char* s, streamsize n, char delim );读取一个字符串,直到遇到字符delim或者读取了n个字符,并将换行符替换为'\0'

3. ignore:istream& ignore (streamsize n = 1, int delim = EOF);

从输入队列中提取字符,并忽略它们,直到提取完前面n个字符或者遇到字符delim为止(delim也提取并忽略)。完成后输入队列中的第一个字符为第n+1个字符或者delim后面个字符

  char first, last;
std::cout << "Please, enter your first name followed by your surname: ";
first = std::cin.get(); // get one character
std::cin.ignore(256,' '); // ignore until space
last = std::cin.get(); // get one character
std::cout << "Your initials are " << first << last << '\n';

输出为:

Please, enter your first name followed by your surname: John Smith
Your initials are JS

4. 其他:peek、read、readsome、putback、unget

5. tellg、seekg

6. sync:int sync();

输入缓冲区同步

三、从输出流中继承的函数

1. put:ostream& put (char c);

输出字符c

2. 其他:write、tellp、seekp、flush

四、保护成员函数(C++11)

=操作、swap

五、从ios类继承来的函数

1. ios::good:bool good() const;

如果错误状态标识(eofbit、failbit、badbit)被设置了返回false,如果没有返回ture

2. ios::eof:bool eof() const;

如果eofbit被设置了返回ture,说明到达了输入末尾了

3. fail:bool fail() const;

如果failbit或者badbit被设置了返回true

4. bad:bool bad() const;

如果badbit被设置了返回true

iostate value
(member constants)
indicates functions to check state flags
good() eof() fail() bad() rdstate()
goodbit No errors (zero value iostate) true false false false goodbit
eofbit End-of-File reached on input operation false true false false eofbit
failbit Logical error on i/o operation false false true false failbit
badbit Read/writing error on i/o operation false false true true badbit

5. ios::!操作

如果failbit或者badbit被设置了返回true

6. ios::clear:void clear (iostate state = goodbit);

为错误状态标识设置新的值

7. ios::fill:char fill() const;返回填充字符

char fill (char fillch);用字符fillch进行填充,返回填充字符

填充输出的空闲空间

  char prev;
std::cout.width (10);
std::cout << 40 << '\n';
prev = std::cout.fill ('x');
std::cout.width (10);
std::cout << 40 << '\n';
std::cout.fill(prev);

输出:

40
xxxxxxxx40

8. 其他:rdstate、setstate、copyfmt、exceptions、imbue、tie、rdbuf、narrow、widen

六、从ios_base类中继承的函数

1. flags:fmtflags flags() const;返回当前格式

fmtflags flags (fmtflags fmtfl);设置新的格式,返回之前的格式

  std::cout.flags ( std::ios::right | std::ios::hex | std::ios::showbase );
std::cout.width (10);
std::cout << 100 << '\n';

输出:      0x64

2. setf:fmtflags setf (fmtflags fmtfl);

fmtflags setf (fmtflags fmtfl, fmtflags mask);

设置格式,返回设置之前的格式

  std::cout.setf ( std::ios::hex, std::ios::basefield );  // set hex as the basefield
std::cout.setf ( std::ios::showbase ); // activate showbase
std::cout << 100 << '\n';
std::cout.unsetf ( std::ios::showbase ); // deactivate showbase
std::cout << 100 << '\n';

输出:

0x64
64

3. unset:void unsetf (fmtflags mask);

清空mask选择的格式

4. precision:streamsize precision() const;返回当前的浮点精度(即小数点位数)

streamsize precision (streamsize prec);设置浮点精度,返回之前的浮点精度

5. width:streamsize width() const;返回当前域宽度

streamsize width (streamsize wide);设置域宽度,返回之前的域宽度

6. 其他:imbue、getloc、xalloc、iword、pword、register_callback、sync_with_stdio

参考:http://www.cplusplus.com/reference/istream/iostream/

输入输出流类iostream常用函数解析的更多相关文章

  1. python重要的第三方库pandas模块常用函数解析之DataFrame

    pandas模块常用函数解析之DataFrame 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器 ...

  2. pandas模块常用函数解析之Series(详解)

    pandas模块常用函数解析之Series 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器输入网 ...

  3. numpy.random模块常用函数解析

    numpy.random模块中常用函数解析 numpy.random模块官方文档 1. numpy.random.rand(d0, d1, ..., dn)Create an array of the ...

  4. Java中math类的常用函数

    Java中math类的常用函数 在 Java 中 Math 类封装了常用的数学运算,提供了基本的数学操作,如指数.对数.平方根和三角函数等 只要在源文件的顶部加上下面这行代码就不必在数学方法名和常量名 ...

  5. java 多线程总结篇2之——Thread类及常用函数

    此片文章主要总结的是Thread类及相关的基础概念和API,首先需要厘清线程调度中的几个基本概念: 一.线程调度的基本方法 1.调整线程优先级:Java线程有优先级,优先级高的线程会获得较多的运行机会 ...

  6. Java的IO输入输出流类的介绍(有图)

    一.字节流 1.InputStream/OutputStream(输入流与输出流几乎一一对应) 读取的方法   int read()   int read(byte[] buffer)   int r ...

  7. Qt QString类及常用函数功能详解

    QString 是 Qt 编程中常用的类,除了用作数字量的输入输出之外,QString 还有很多其他功能,熟悉这些常见的功能,有助于灵活地实现字符串处理功能. QString 存储字符串釆用的是 Un ...

  8. tensorflow常用函数解析

    一.tf.transpose函数的用法 tf.transpose(input, [dimension_1, dimenaion_2,..,dimension_n]):这个函数主要适用于交换输入张量的不 ...

  9. Path模块部分常用函数解析——NodeJS

    官网地址:https://nodejs.org/api/path.html path.resolve([...paths])# Added in: v0.3.4 参数[...paths]: <S ...

随机推荐

  1. 未来手机Alo即将问世!全息投影手机的新高峰!全息3d 网

    文章来源:网络         编辑:大熊 [摘要]全息投影手机很早就开始炒,网络上的概念机也是丛出不穷,那么这款出自法国的概念机又是多么的奇葩?全息 3d 网带你一探究竟. 据外媒报道,在不久将来语 ...

  2. 记一次阿里云Linux服务器安装.net core sdk的问题以及解决方法

    因为公司领导要求新的项目能跨平台部署,也就是说能部署到Linux服务器上,故新的项目采用了Asp.net mvc core 1.1 进行开发.开发过程一切都比较顺利,然后在之前申请试用的一台微软Azu ...

  3. JavaScript中几个相似方法对比

    一.substring与substr   substring substr 概述 返回字符串两个索引之间(或到字符串末尾)的子串 返回字符串从指定位置开始到指定长度的子串 语法 参数 indexSta ...

  4. CI Weekly #14 | 如何搭建合适的持续交付开发流程?

    时隔 10 个月,flow.ci 开始正式收费上线.为感谢对我们的内测支持,所有内测用户可继续免费使用基础版 30 天,截止至 3 月 15 日失效.欢迎随时告诉我们你对收费版 flow.ci 的反馈 ...

  5. Java编程规范(二)

    二.格式规范 在上一篇的java编程规范(一)中我们讲述了在Java编码中的一般原则,虽然这些原则并不涉及具体的代码规范,但是这些原则却是我们在Java开发过程中所应该遵循的规范与思想.今天我们将学习 ...

  6. 第24篇 js小知识和“坑”

    前面说了说了js的相关知识,基本上除了语法外,把项目常用的知识做了一个梳理,现在说下js的其它方面的知识,这些知识不成体系,属于不理解对于一般开发没什么太多影响,但如果理解清楚,可以更好去开发. js ...

  7. 关于脚本化css

    ---恢复内容开始--- 想把自己认为的最重要的,最有用的几块写上,以后会边学边总结完善. 1.首先我们通过JavaScript可以获取到我们想要获取的元素的样式.而这个样式并非单独的哪一个部分的规则 ...

  8. js中盒子模型常用的属性你还记得几个?

    //clientWidth;//内容宽度Width+左右填充padding//clientHeight;//内容高度height+上下填充padding//clientLeft;//左边框的宽度相当于 ...

  9. 程序员的自我修养:高效使用Google解决问题

    如果票选近二十年最伟大的发明,我相信搜索引擎肯定会占据一个不容小觑的位置,它不单是一项发明,更是一项成就,最大程度消灭了信息的不平等.既然人人都可以接触到海量的信息,那么衡量信息财富多寡就只剩下技巧这 ...

  10. mfc---CFileDialog的过滤器

    CFileDialog的过滤器每种类型由两部分组成“tda|*.tda”,两种类型之间用“|”隔开