C ++ Io stream class library from scratch (1): basic operations of the stream class library inheritance system (IO stream, file stream, stream) and string stream

Source: Internet
Author: User
Tags split words

I. Io and stream

Data input and output (I/O for input/output)
The input and output of standard input devices and standard output devices are referred to as standard I/O
The input and output of files stored on an external disk are referred to as files I/O.
The input and output of the specified string bucket in the memory are referred to as string I/O.


The process of data input and output can be visually viewed as a stream.
The operation for retrieving data from a stream is called an "extract" (input) operation.
An operation that adds data to a stream is called an "Insert" (output) operation.
Standard input/output stream
File stream
String stream


Ii. Stream class library inheritance system and four input and output objects

The stream library has two parallel base classes, directed buffering: streambuf and iOS classes. All stream classes use one of the two as the base classes.

The streambuf class provides low-level operations on the buffer zone: Setting the buffer zone and storing/retrieving characters in the buffer pointer operation zone.
The ios_base and iOS types record the stream status and support formatting or non-formatting of the buffer input/output of streambuf.
Strstreambuf: stores character sequences with strings. Extended Management of streambuf's buffer extraction and insertion
Filebuf: stores character sequences using files. Including opening files, reading/writing, and searching characters


For example:


C ++ defines four class objects for standard I/O operations: Cin, cout, cerr, and clog.

CIN is the object of the istream stream class, representing the keyboard of the standard input device, and the last three objects of the ostream stream class

Cout stands for standard output device display

Cerr and clog share the same meaning, indicating that the output device displays the error message.


3. Operations on ostream and istream streams

(1) ostream operations:

1. Operator <

<The operation returns a reference to an ostream object, so it can be used continuously.

2. Put ()

Output Single Character

Returns an ostream object reference.

Cout. Put ('H'). Put ('I ');


3. Write ()

Write (BUF, Len)

Write () returns an ostream object reference.

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;
}

(2) istream stream operations:

1. opeartor> operation

<The operation returns a reference to an ostream object, so it can be used continuously.


2. Get ()

Get () operation:

Read a single character

Returns an integer.

ASCII code of a character

Get (char &) operation:

Read a single character

Returns an istream object reference.


3. Getline ()

Read a row

Enter the Enter key

Returns an istream object reference.
Difference between Getline () and>:

Char string1 [256],

Cin. Getline (string1, 256); // get a whole line, ending with '\ 0'

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


4. Read ()

Read (BUF, Len)
Returns an istream object reference.
No error occurred while reading white space characters (including '\ n')


5. Peek () and putpack ()

Peek: view but not read
Putback: adds a character to the stream.

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 success;
// Char CH2;
// Cin. Get (SUCCESS). Get (CH2 );
// Cout <strong <"" <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;
}

Ii. Basic operations on string streams

Istringstream, derived from istream, provides the string READ function
Ostringstream, derived from ostream, provides the string Writing Function
Stringstream, derived from iostream, provides the string read/write function

(1) Split words

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;
}



(2) Conversion between string and double types

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;

}

(3) Implement functions similar to sscanf and 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;

}

The output is 192.168.0.100.


Refer:

C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.