從零開始學C++之IO流類庫(一):流類庫繼承體系(IO流,檔案流,串流)和 字串流的基本操作

來源:互聯網
上載者:User

一、IO、流

資料的輸入和輸出(input/output簡寫為I/O)
對標準輸入裝置和標準輸出裝置的輸入輸出簡稱為標準I/O
對在外存磁碟上檔案的輸入輸出簡稱為檔案I/O
對記憶體中指定的字串儲存空間的輸入輸出簡稱為串I/O


資料輸入輸出的過程,可以形象地看成流
從流中擷取資料的操作稱為“提取”(輸入)操作
向流中添加資料的操作稱為“插入”(輸出)操作
標準輸入輸出資料流
檔案流
字串流


二、流類庫繼承體系、四個輸入輸出對象

流庫具有兩個平行的基類、向緩衝:streambuf 和 ios 類,所有流類均以兩者之一作為基類

streambuf 類提供對緩衝區的低級操作:設定緩衝區、對緩衝區指標操作區存/取字元
ios_base、ios 類記錄流狀態,支援對streambuf 的緩衝區輸入/輸出的格式化或非格式化轉換
strstreambuf:使用串儲存字元序列。擴充 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++編程規範

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.