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个数 ...
随机推荐
- Murano Setup Steps
1. Select a Linux Distribution Only Ubuntu 14.04 (Trusty), Fedora 21 (or Fedora 22) and CentOS/RHEL ...
- Windows 编程
在WndProc函数中 最好不要出现WM_SYSCOMMAND消息, 如果有了这个消息, 可能我们对创建出来的窗口就什么都管不了了, 因为我们阻碍了DefWndProc函数去处理它 不在.rc文件中添 ...
- FastDFS 基础知识
FastDFS是一个开源的轻量级分布式文件系统,它用纯C语言实现,支持Linux.FreeBSD.AIX等UNIX系统.它只能通过专有API对文件进行存取访问,不支持POSIX接口方式,不能mount ...
- agc001E - BBQ Hard(dp 组合数)
题意 题目链接 Sol 非常妙的一道题目. 首先,我们可以把\(C_{a_i + b_i + a_j + b_j}^{a_i + a_j}\)看做从\((-a_i, -b_i)\)走到\((a_j, ...
- extjs 6
因为最近公司要写一个项目前台所以开始学习extjs前端框架,希望一起共勉. 那么我们的教程就从 Hello World 讲起. helloWorld.js Ext.onReady(function ...
- vue中 eCharts 自适应容器
在 vue 脚手架开发中,echarts图表自适应容器的方法: 父组件: <template> <div class="statistics_wrap"> ...
- Android基础Activity篇——销毁活动
销毁活动只需要添加 finish(); 这个方法即可.相当于back键.
- Azure 4月新公布
Azure 4 月新发布:Linux 上的 Azure Service Fabric 公共预览版正式发布:Azure 物联网套件新增设备管理功能:计量名称变更 Linux 上的 Azure Servi ...
- [转]linux tcp/ip调优
LINUX tcp/ip性能调优 On 2011年03月15日, in linux, tips, by netoearth 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接 ...
- notepad++ TextFX替代
notepad++目前的版本已没有了TextFX插件,插件的原作者在2008年的时候已停止维护.目前官方的意思是用以下插件替代,见 http://docs.notepad-plus-plus.org/ ...