一、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]

 C++ Code 
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)
{
    int n = 100;
    int n2 = 200;
    cout << n << " " << n2 << endl;

cout.put('H');
    cout.put('i');
    cout.put(' ');
    cout.put('H').put('i').put('\n');

char buf[] = "test!!!!!";
    cout.write(buf, 5);

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:将一个字符添加到

 C++ Code 
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;
    //char ch;

//cin>>n>>ch;
    //cout<<"n="<<n<<" "<<"ch="<<ch<<endl;

//int ch = cin.get();
    //cout<<ch<<endl;

//char ch1;
    //char ch2;
    //cin.get(ch1).get(ch2);
    //cout<<ch1<<" "<<ch2<<endl;

char buf[10] = {0};
    cin.getline(buf, 10);
    cout << buf << endl;

//char buf[10] = {0};
    //cin>>buf;
    //cout<<buf<<endl;

//char buf[10] = {0};
    //cin.read(buf, 5);
    //cout<<buf<<endl;

/*char c[10], c2, c3;

c2 = cin.get( );
    c3 = cin.get( );
    cin.putback( c2 );
    cin.getline( &c[0], 10);
    cout << c << endl;*/

return 0;
}

二、字符串流的基本操作

istringstream,由istream派生而来,提供读string的功能
ostringstream,由ostream派生而来,提供写string的功能
stringstream,由iostream派生而来,提供读写string的功能

(一)、分割单词

 C++ Code 
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)
{
    string line;
    string word;

while (getline(cin, line))
    {
        istringstream iss(line);

while (iss >> word)
            cout << word << "#";
        cout << endl;
    }
    return 0;
}

(二)、字符串与double 类型互相转换

 C++ Code 
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)
{
    ostringstream oss;
    oss << val;

return oss.str(); // return string copy of character array
}

double strtodouble(const string &str)
{
    istringstream iss(str);
    double val;
    iss >> val;
    return val;
}

int main(void)
{
    double val = 55.55;

string str = doubletostr(val);
    cout << str << endl;

str = "123.123";
    val = strtodouble(str);
    cout << val << endl;
    return 0;

}

(三)、实现类似sscanf, sprinft 的功能

 C++ Code 
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;
    //sscanf,sprintf;

//istringstream iss("192,168,0,100");
    //int v1;
    //int v2;
    //int v3;
    //int v4;
    //char ch;
    //iss>>v1>>ch>>v2>>ch>>v3>>ch>>v4;

//ch = '.';
    //ostringstream oss;
    //oss<<v1<<ch<<v2<<ch<<v3<<ch<<v4;

//cout<<oss.str()<<endl;

string buf("192,168,0,100");
    stringstream ss(buf);
    int v1;
    int v2;
    int v3;
    int v4;
    char ch;
    ss >> v1 >> ch >> v2 >> ch >> v3 >> ch >> v4;

ch = '.';
    stringstream ss2;
    ss2 << v1 << ch << v2 << ch << v3 << ch << v4;

cout << ss2.str() << endl;

return 0;

}

输出为192.168.0.100

参考:

C++ primer 第四版
Effective C++ 3rd
C++编程规范

最新文章

  1. SQL通过ContentTypeID找使用了内容类型的列表库
  2. 关于PHP语言
  3. windows下Bullet 2.82编译安装(Bullet Physics开发环境配置)
  4. 『U3D学习』破坏神回忆图&lt;二&gt;技能系统
  5. 【巩固】bootstrap笔记二
  6. Redo丢失场景和处理方法
  7. html5游戏引擎phaser官方示例学习
  8. C语言--通用类型栈
  9. IDA*
  10. JavaScript高级程序设计39.pdf
  11. Git 2.7: 一个新的带来许多新特性和性能提升的主要版本
  12. hibernate 查询、二级缓存、连接池
  13. 用javap命令反编译来分析字符串问题
  14. 基于visual Studio2013解决C语言竞赛题之0513字符拷贝
  15. bootstrap-导航总结
  16. 手工脱壳之FSG压缩壳-IAT表修复
  17. netty支持的各种socketchannel实现及比较
  18. Numpy random arange zeros
  19. Wireshark数据抓包教程之认识捕获分析数据包
  20. Php优化方案

热门文章

  1. iScroll4插件的使用实例
  2. C# http Get/POST请求封装类
  3. 百度地图js小结
  4. mysql重启,重启释放ibtmp1
  5. Python学习(六)模块 —— 第三方库
  6. OpenCV学习(11) 图像的腐蚀与膨胀(2)
  7. Informatica 常用组件Source Qualifier之二 默认查询
  8. 什么是vBlock
  9. 【Linux】shell判空
  10. (剑指Offer)面试题42:左旋转字符串