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库的更多相关文章

  1. [APUE]标准IO库(下)

    一.标准IO的效率 对比以下四个程序的用户CPU.系统CPU与时钟时间对比 程序1:系统IO 程序2:标准IO getc版本 程序3:标准IO fgets版本 结果: [注:该表截取自APUE,上表中 ...

  2. [APUE]标准IO库(上)

    一.流和FILE对象 系统IO都是针对文件描述符,当打开一个文件时,即返回一个文件描述符,然后用该文件描述符来进行下面的操作,而对于标准IO库,它们的操作则是围绕流(stream)进行的. 当打开一个 ...

  3. 文件IO函数和标准IO库的区别

    摘自 http://blog.chinaunix.net/uid-26565142-id-3051729.html 1,文件IO函数,在Unix中,有如下5个:open,read,write,lsee ...

  4. 《APUE》-第五章标准IO库

    大多数UNIX应用程序都使用I/O库,本章说明了该库所包含的所有函数,以及某些实现细节和效率方面的考虑.同时需要重点关注标准I/O使用了缓冲的技术,但同时也是因为它的出现,产生了很多细节上的问题. 流 ...

  5. C++ Primer 读书笔记: 第8章 标准IO库

    第8章 标准IO库 8.1 面向对象的标准库 1. IO类型在三个独立的头文件中定义:iostream定义读写控制窗口的类型,fstream定义读写已命名文件的类型,而sstream所定义的类型则用于 ...

  6. 标准模板库——IO库

    IO库设施: . istream(输入流)类型,提供输入操作. . ostream(输出流)类型,提供输出操作. . cin,一个istream对象,从标准输入读取数据. . cout,一个ostre ...

  7. 高级UNIX环境编程5 标准IO库

    标准IO库都围绕流进进行的 <stdio.h><wchar.h> memccpy 一般用汇编写的 ftell/fseek/ftello/fseeko/fgetpos/fsetp ...

  8. c++ primer 学习杂记3【标准IO库】

    第8章 标准IO库 发现书中一个错误,中文版p248 流状态的查询和控制,举了一个代码例子: int ival; // read cin and test only for EOF; loop is ...

  9. C/C++基础----标准库几个工具库tuple,bitset,正则表达式,随机数,IO库

    tuple tuple可以有任意多个成员 默认初始化,值初始化 构造函数是explicit,必须直接初始化 make_tuple(v1,v2,-,vn) get<i> (t) 返回第i个数 ...

随机推荐

  1. [转]jQuery AJAX pagination plugin with ASP.NET Server Side

    本文转自:http://do-web.com/jpaging/usage How does it work? 1. In order to implement the plugin, you need ...

  2. 分布式任务框架elastic-job 学习笔记

    官方资料:https://github.com/dangdangdotcom/elastic-job ------------------------------------------------- ...

  3. go语言初始化内部结构体3中方式

    package main import ( "fmt" ) type User struct { Id int Name string Age int } type Manger ...

  4. React.js 小书 Lesson4 - 前端组件化(三):抽象出公共组件类

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson4 转载请注明出处,保留原文链接和作者信息. 为了让代码更灵活,可以写更多的组件,我们把这种模 ...

  5. 解析xml数据存入bean映射到数据库的 需求解决过程

    解析xml数据存入bean映射到数据库的 需求解决过程2017年12月19日 15:18:57 守望dfdfdf 阅读数:419 标签: xmlbean 更多个人分类: 工作 问题编辑版权声明:本文为 ...

  6. 内存分配详解 malloc, new, HeapAlloc, VirtualAlloc,GlobalAlloc

    很多地方都会使用内存,内存使用过程中操作不当就容易崩溃,无法运行程序,上网Google学习一下,了解整理下他们之间的区别以及使用 ,获益匪浅 0x01 各自的定义和理解 (1)先看GlobalAllo ...

  7. 应该知道的一些Markdown语法

    目录 快速输入标题 斜体和粗体,删除线 分隔线 外链接 无序列表 有序列表 定义型列表 插入图片 文字引用 标签分类 表格 行内代码块 代码段 注脚 待办事宜 Todo 列表 显示当前文章的目录 快速 ...

  8. laravel下的ORM数据映射之自由畅想

    此处以Model::get()方法和Model::first()方法为例 public static function get($data=[]){//默认是空数组 if(count($data)== ...

  9. dedecms无法下载远程jpeg图片 织梦不能提取文章内容中的jpeg图片生成缩略图

    文件:/dede/inc/inc_archives_functions.php 代码: preg_match_all("/(src)=[\"|'| ]{0,}([^>]*\. ...

  10. IIS7 配置SSL 绑定主机头

    IIS7下面默认HTTPS绑定是无法指定主机头的,我们可以通过手工修改IIS配置来实现主机头绑定. 首先停止IIS服务. 然后打开C:/Windows/system32/inetsrv/config/ ...