流类库继承体系(IO流,文件流,串流)和 字符串流的基本操作
一、IO、流
数据的输入和输出(input/output简写为I/O)
对标准输入设备和标准输出设备的输入输出简称为标准I/O
对在外存磁盘上文件的输入输出简称为文件I/O
对内存中指定的字符串存储空间的输入输出简称为串I/O
数据输入输出的过程,可以形象地看成流
从流中获取数据的操作称为“提取”(输入)操作
向流中添加数据的操作称为“插入”(输出)操作
标准输入输出流
文件流
字符串流
二、流类库继承体系、四个输入输出对象
流库具有两个平行的基类:streambuf 和 ios 类,所有流类均以两者之一作为基类
streambuf 类提供对缓冲区的低级操作:设置缓冲区、对缓冲区指针操作区存/取字符
ios_base、ios 类记录流状态,支持对streambuf 的缓冲区输入/输出的格式化或非格式化转换
stringbuf:使用串保存字符序列。扩展 streambuf 在缓冲区提取和插入的管理
filebuf:使用文件保存字符序列。包括打开文件;读/写、查找字符
如下图:
C++为用户进行标准I/O操作定义了四个类对象: cin,cout,cerr和clog
cin为istream流类的对象,代表标准输入设备键盘,后三个为ostream流类的对象
cout代表标准输出设备显示器
cerr和clog含义相同,均代表错误信息输出设备显示器
三、ostream流 的操作,istream 流的操作
(一)、ostream流 的操作:
1、operator <<
<<操作返回一个ostream对象的引用,所以可以连续使用
2、put( )
输出单个字符
返回一个ostream对象的引用
cout.put(‘H’).put(‘i’);
3、write( )
write(buf, len)
write( )返回一个ostream对象的引用
cout.write (buf, len) //char buf[len]
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream>
using namespace std; int main(void) cout.put('H'); char buf[] = "test!!!!!"; return 0; |
(二)、istream流 的操作:
1、opeartor>>操作
<<操作返回一个ostream对象的引用,所以可以连续使用
2、get( )
get( )操作:
读取单个字符
返回一个整数
字符的ASCII码
get(char&)操作:
读取单个字符
返回一个istream对象的引用
3、getline( )
读取一行
遇到回车键
返回istream对象的引用
getline()操作与>>的区别:
char string1 [256],
cin.getline(string1, 256); //get a whole line, 以'\0'结尾
cin >> string1; //stop at the 1st blank space
4、read( )
read(buf, len)
返回一个istream对象的引用
对空白字符(包括'\n')照读不误
5、peek( ) 与 putpack()
peek:查看而不读取
putback:将一个字符添加到
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <iostream>
using namespace std; int main(void) //int n; //cin>>n>>ch; //int ch = cin.get(); //char ch1; char buf[10] = {0}; //char buf[10] = {0}; //char buf[10] = {0}; /*char c[10], c2, c3; c2 = cin.get( ); return 0; |
二、字符串流的基本操作
istringstream,由istream派生而来,提供读string的功能
ostringstream,由ostream派生而来,提供写string的功能
stringstream,由iostream派生而来,提供读写string的功能
(一)、分割单词
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream>
#include <sstream> using namespace std; int main(void) while (getline(cin, line)) while (iss >> word) |
(二)、字符串与double 类型互相转换
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream>
#include <sstream> using namespace std; string doubletostr(double val) return oss.str(); // return string copy of character array double strtodouble(const string &str) int main(void) string str = doubletostr(val); str = "123.123"; } |
(三)、实现类似sscanf, sprinft 的功能
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <iostream>
#include <sstream> using namespace std; int main(void) //192,168,0,100; //istringstream iss("192,168,0,100"); //ch = '.'; //cout<<oss.str()<<endl; string buf("192,168,0,100"); ch = '.'; cout << ss2.str() << endl; return 0; } |
输出为192.168.0.100
参考:
C++ primer 第四版
Effective C++ 3rd
C++编程规范
最新文章
- SQL通过ContentTypeID找使用了内容类型的列表库
- 关于PHP语言
- windows下Bullet 2.82编译安装(Bullet Physics开发环境配置)
- 『U3D学习』破坏神回忆图<;二>;技能系统
- 【巩固】bootstrap笔记二
- Redo丢失场景和处理方法
- html5游戏引擎phaser官方示例学习
- C语言--通用类型栈
- IDA*
- JavaScript高级程序设计39.pdf
- Git 2.7: 一个新的带来许多新特性和性能提升的主要版本
- hibernate 查询、二级缓存、连接池
- 用javap命令反编译来分析字符串问题
- 基于visual Studio2013解决C语言竞赛题之0513字符拷贝
- bootstrap-导航总结
- 手工脱壳之FSG压缩壳-IAT表修复
- netty支持的各种socketchannel实现及比较
- Numpy random arange zeros
- Wireshark数据抓包教程之认识捕获分析数据包
- Php优化方案