c++标准之IO库
1.面向对象的标准库
2.多种IO标准库工具
- istream,提供输入操作
- ostream,提供输出操作
- cin:读入标准输入的istream对象.全局对象extern std::istream cin;定义于头文件
<iostream>
- cout:写到标准输出的ostream对象
- cerr:输出标准错误的ostream对象。常用语程序错误信息
- >>,用于从istream对象中读入输入.从左到右
- <<,用于把输出写到ostream对象中
- getline,功能是从istream对象读取一个单词,然后写入string对象中
3.IO类之间的关系
4.IO对象不可复制或赋值
流对象不能存储在vector或其他容器中。
形参或返回类型也不能为流类型,如果需要,则必须传递或返回指向该对象的指针或引用,而且不能是const引用,因为读写一个IO对象会改变其状态。
5.IO库的条件状态
流的状态由bad,fail,eof,good操作揭示。
标志
- iostate:机器相关的类型,表达条件状态。
- badbit:系统级的故障
- failbit:可恢复的错误,比如 把字符输入到数值变量中。
- eofbit:遇到文件结束符,fail也置位
- goodbit:流未处于错误状态,保证为0.
函数(s表示流
- s.bad(): 若badbit置位,返回true
- s.fail(): 若failbit置位,返回true
- s.eof(): 若eofbit置位,返回true
- s.good(): 若goodbit置位,返回true
- s.clear():流的所有条件状态位复位,将流的状态设为有效,void
- s.clear(flag):指定位复位,void
- s.setstate(flag):指定位 置位,void
- s.rdstate(): 返回当前流的条件状态,s.iostate
《cpp primer》p280 :只有当一个流处于无错状态时,才能从他读写数据。在使用流之前,应该检查它的状态,通常用while循环来检查。>>表达式返回的是流的状态。
使用good()和fail()是检查流总体状态的正确方法。将流当作条件的代码等价:!fail();
if(cin)
//ok to use cin, it is in a valid state
while(cin >> word)
//ok:read operation successful…
6.string流 <sstream>
公开成员函数 std::basic_stringstream::str
std::basic_string<CharT,Traits,Allocator> str() const; |
(1) | |
void str(const std::basic_string<CharT,Traits,Allocator>& new_str); |
(2) |
#include <sstream>
#include <iostream>
int main()
{
int n;
std::istringstream in; // 亦可使用 in("1 2")
in.str("1 2");
in >> n;
std::cout << "after reading the first int from \"1 2\", the int is "
<< n << ", str() = \"" << in.str() << "\"\n";
std::ostringstream out("1 2");
out << 3;
std::cout << "after writing the int '3' to output stream \"1 2\""
<< ", str() = \"" << out.str() << "\"\n";
std::ostringstream ate("1 2", std::ios_base::ate);
ate << 3;
std::cout << "after writing the int '3' to append stream \"1 2\""
<< ", str() = \"" << ate.str() << "\"\n";
}
after reading the first int from "1 2", the int is 1, str() = "1 2"
after writing the int '3' to output stream "1 2", str() = "3 2"
after writing the int '3' to append stream "1 2", str() = "1 23"
读取输入流的个人信息
struct PersonInfo
{
/* data */
string name;
vector<string> phones;
};
string line,word;
vector<PersonInfo> people;
while(getline(cin,line)){
PersonInfo info;
istringstream record(line);//line拷贝到record中
record >>info.name;
while(record>>word)
info.phones.push_back(word);
people.push_back(info);
/* code */
}
c++标准之IO库的更多相关文章
- [APUE]标准IO库(下)
一.标准IO的效率 对比以下四个程序的用户CPU.系统CPU与时钟时间对比 程序1:系统IO 程序2:标准IO getc版本 程序3:标准IO fgets版本 结果: [注:该表截取自APUE,上表中 ...
- [APUE]标准IO库(上)
一.流和FILE对象 系统IO都是针对文件描述符,当打开一个文件时,即返回一个文件描述符,然后用该文件描述符来进行下面的操作,而对于标准IO库,它们的操作则是围绕流(stream)进行的. 当打开一个 ...
- 文件IO函数和标准IO库的区别
摘自 http://blog.chinaunix.net/uid-26565142-id-3051729.html 1,文件IO函数,在Unix中,有如下5个:open,read,write,lsee ...
- 《APUE》-第五章标准IO库
大多数UNIX应用程序都使用I/O库,本章说明了该库所包含的所有函数,以及某些实现细节和效率方面的考虑.同时需要重点关注标准I/O使用了缓冲的技术,但同时也是因为它的出现,产生了很多细节上的问题. 流 ...
- C++ Primer 读书笔记: 第8章 标准IO库
第8章 标准IO库 8.1 面向对象的标准库 1. IO类型在三个独立的头文件中定义:iostream定义读写控制窗口的类型,fstream定义读写已命名文件的类型,而sstream所定义的类型则用于 ...
- 标准模板库——IO库
IO库设施: . istream(输入流)类型,提供输入操作. . ostream(输出流)类型,提供输出操作. . cin,一个istream对象,从标准输入读取数据. . cout,一个ostre ...
- 高级UNIX环境编程5 标准IO库
标准IO库都围绕流进进行的 <stdio.h><wchar.h> memccpy 一般用汇编写的 ftell/fseek/ftello/fseeko/fgetpos/fsetp ...
- c++ primer 学习杂记3【标准IO库】
第8章 标准IO库 发现书中一个错误,中文版p248 流状态的查询和控制,举了一个代码例子: int ival; // read cin and test only for EOF; loop is ...
- C/C++基础----标准库几个工具库tuple,bitset,正则表达式,随机数,IO库
tuple tuple可以有任意多个成员 默认初始化,值初始化 构造函数是explicit,必须直接初始化 make_tuple(v1,v2,-,vn) get<i> (t) 返回第i个数 ...
随机推荐
- httpUrlConnection连接网络的用法(用到了handle传递消息,在主线程中更新UI)
由于httpclient在Android5.0以后已经过时,所以官方推荐使用httpUrlConnection来连接网络,现将该连接的基本方法展示,如下 注意:记得加入<uses-permiss ...
- HDU 4355——Party All the Time——————【三分求最小和】
Party All the Time Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- vue中添加echarts
方法一:全局引入echarts 步骤: 1.全局安装 echarts依赖. cnpm install echarts -- save 2.引入echarts模块,在Vue项目的main. ...
- dll托管于非托管
托管的DLL组件可以在VS 直接添加引用,在使用using dll的文件命名空间就可以使用 非托管的DLL组件,只能通过using system.Runtime.InteropServices:引入 ...
- webpack中package.json中的Script
初始化项目 cnpm init -y ,会自动生成一个package.json文件:主要是显示项目的名称.版本.作者.协议等信息 在package.json中scripts中配置:(运行简化) 开发模 ...
- Spring Boot + Redis 实现Shiro集群
为实现Web应用的分布式集群部署,要解决登录session的统一.本文利用shiro做权限控制,redis做session存储,结合spring boot快速配置实现session共享. 1.引入相关 ...
- Excel数据导入数据库
maven依赖 <!--excel相关依赖--> <dependency> <groupId>org.apache.poi</groupId> < ...
- 【起航计划 025】2015 起航计划 Android APIDemo的魔鬼步伐 24 App->Notification->Notifying Service Controller service中使用Notification
这个例子介绍了如何在Service中使用Notification,相关的类为NotifyingController和NotifyingService. 在Service中使用Notification的 ...
- 【起航计划 023】2015 起航计划 Android APIDemo的魔鬼步伐 22 App->Menu->Inflate from XML 使用xml资源展示菜单
本例MenuInflateFromXml.java演示了如何从Menu 资源(XML 定义)展开菜单项.这个例子的onCreate 采用了使用代码来创建Activity 界面的方法 而通常的方法是采用 ...
- Cocos2d-x v3.1项目创建(三)
Cocos2d-x v3.1项目创建(三) Cocos2d-x官方为我们提供了用于创建.编译.运行和部署的一套命令行的工具集,也就是上篇文章中我们所提到的Cocos2d-Console,它位于我们的引 ...