当前位置: 首页 > news >正文

输入输出流类iostream常用函数解析

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

一.成员类型

1. ios::fmtflags

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

fieldmember constanteffect when set
independent flagsboolalpharead/write bool elements as alphabetic strings (true and false).
showbasewrite integral values preceded by their corresponding numeric base prefix.
showpointwrite floating-point values including always the decimal point.
showposwrite non-negative numerical values preceded by a plus sign (+).
skipwsskip leading whitespaces on certain input operations.
unitbufflush output after each inserting operation.
uppercasewrite uppercase letters replacing lowercase letters in certain insertion operations.
numerical base 
(basefield)
decread/write integral values using decimal base format.
hexread/write integral values using hexadecimal base format.
octread/write integral values using octal base format.
float format 
(floatfield)
fixedwrite floating point values in fixed-point notation.
scientificwrite floating-point values in scientific notation.
adjustment 
(adjustfield)
internalthe output is padded to the field width by inserting fill characters at a specified internal point.
leftthe output is padded to the field width appending fill characters at the end.
rightthe output is padded to the field width by inserting fill characters at the beginning.

 2. ios_base::iostate

flag valueindicates
eofbitEnd-Of-File reached while performing an extracting operation on an input stream.
failbitThe last input operation failed because of an error related to the internal logic of the operation itself.
badbitError due to the failure of an input/output operation on the stream buffer.
goodbitNo 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)
indicatesfunctions to check state flags
good()eof()fail()bad()rdstate()
goodbitNo errors (zero value iostate)truefalsefalsefalsegoodbit
eofbitEnd-of-File reached on input operationfalsetruefalsefalseeofbit
failbitLogical error on i/o operationfalsefalsetruefalsefailbit
badbitRead/writing error on i/o operationfalsefalsetruetruebadbit

 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/

 

转载于:https://www.cnblogs.com/shrimp-can/p/5657192.html

相关文章:

  • 自拟定端口号
  • es的写入过程
  • Kubernetes 集群的两种部署过程(daemon部署和容器化部署)以及glusterfs的应用!...
  • mysql常用的数据备份方案
  • Bitbucket Pipelines在Atlassian的Bitbucket云上提供持续交付功能
  • 下拉菜单的编辑
  • 【乐畅】工作积累 ---- 后台倒计时 ( 体力恢复功能 )
  • block防止循环引用的技巧
  • 分布式部署lamp,phpmyadmin,wordpress的安装与应用
  • == 和 equals()的区别
  • Java基本语法-----java常量
  • 开发中的对控件大小 颜色等默认状态的意识
  • 面试资料
  • Linux 常用命令(一)
  • [转载] FFMPEG之AVRational TimeBase成员理解
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • 【347天】每日项目总结系列085(2018.01.18)
  • 【剑指offer】让抽象问题具体化
  • docker容器内的网络抓包
  • js继承的实现方法
  • leetcode388. Longest Absolute File Path
  • Linux gpio口使用方法
  • mysql 数据库四种事务隔离级别
  • oldjun 检测网站的经验
  • PHP CLI应用的调试原理
  • ReactNative开发常用的三方模块
  • Redis 中的布隆过滤器
  • 基于OpenResty的Lua Web框架lor0.0.2预览版发布
  • 基于Volley网络库实现加载多种网络图片(包括GIF动态图片、圆形图片、普通图片)...
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 用mpvue开发微信小程序
  • Java数据解析之JSON
  • 如何正确理解,内页权重高于首页?
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • #我与Java虚拟机的故事#连载06:收获颇多的经典之作
  • (4)事件处理——(2)在页面加载的时候执行任务(Performing tasks on page load)...
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • (附源码)ssm失物招领系统 毕业设计 182317
  • (实战篇)如何缓存数据
  • (五)c52学习之旅-静态数码管
  • (一)UDP基本编程步骤
  • (转)大型网站架构演变和知识体系
  • (转)拼包函数及网络封包的异常处理(含代码)
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • .CSS-hover 的解释
  • .Family_物联网
  • .FileZilla的使用和主动模式被动模式介绍
  • .gitignore
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .NET MVC第三章、三种传值方式
  • .NET 读取 JSON格式的数据
  • .NET/C# 编译期能确定的字符串会在字符串暂存池中不会被 GC 垃圾回收掉
  • .Net6使用WebSocket与前端进行通信
  • .set 数据导入matlab,设置变量导入选项 - MATLAB setvaropts - MathWorks 中国