流类库继承体系(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]

 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(' ');

    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, 以''结尾

cin >> string1;    //stop at the 1st blank space


4、read( )

read(buf, len)
返回一个istream对象的引用
对空白字符(包括' ')照读不误


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++编程规范

原文地址:https://www.cnblogs.com/alantu2018/p/8471153.html