C ++-style file input and output streams

Source: Internet
Author: User

Note:

The output is mainly completed by the overloaded left shift operator (<), and the input is mainly completed by the overloaded right shift operator (>.

> A indicates to put data into object.
<A indicates to extract the data stored in object.

Next, let's take a look at the C ++ style stream control. c ++ introduces three classes: ostringstream, istringstream, and stringstream, which must be created using them.

The object must contain the sstream. h header file.

The istringstream class is used to perform input operations for C ++-style streaming.
The stringstream class also supports the input and output operations of C ++-style streaming.
The strstream class also supports the input and output operations of C-style streaming.
The istringstream class is derived from istream (input stream class) and stringstreambase (C ++ string stream base class). ostringstream is from ostream (output

Stream class) and stringstreambase (C ++ string stream base class) are derived from, stringstream is from iostream (input and output stream class) and stringstreambase (

C ++ string stream base class.

Istringstream is constructed from a string object. The istringstream class reads characters from a string object.
Istringstream constructor prototype is as follows:
Istringstream: istringstream (string Str );

# Include <iostream>
# Include <sstream>
Using namespace STD;
Int main ()
{
Istringstream istr;
Istr. STR ("1 56.7 ");
// The above two processes can be simply written as istringstream istr ("1 56.7 ");
Cout <istr. STR () <Endl;
Int;
Float B;
Istr>;
Cout <A <Endl;
Istr> B;
Cout <B <Endl;
System ("pause ");
}

In the preceding example, when constructing a string stream, spaces are the internal demarcation of string parameters. In this example, the input "value assignment" Operation of object A and object B proves that the string is empty.

Lattice is the decomposition point of integer and floating point data. Using the method of Division acquisition, we have actually completed the splitting and conversion process from string to integer and floating point objects.

.

The use of the STR () member function allows the istringstream object to return a string (for example, the output operation in this example (cout <istr. STR ();).

Ostringstream is also constructed by a string object. The ostringstream class inserts characters into a string.
The prototype of ostringstream constructor is as follows:
Ostringstream: ostringstream (string Str );

The sample code is as follows:

# Include <iostream>
# Include <sstream>
# Include <string>
Using namespace STD;
Int main ()
{
Ostringstream ostr;
// Ostr. STR ("ABC"); // If a string parameter is set during the construction, the original data is modified instead of the end of the growth operation.

Growth
Ostr. Put ('D ');
Ostr. Put ('E ');
Ostr <"FG ";

String gstr = ostr. STR ();
Cout <gstr;
System ("pause ");
}

In the above code, we can use the put () or left shift operator to continuously insert a single character or string to ostr, and return the increasing complete word through the STR () function.

Character string data, but it is worth noting that when constructing a string data already exists in the object, the growth operation will not start from the end,

Modify the original data, and the excess data increases.

For stringstream, I don't need to talk about it. As you know, it is used for input and output of C ++-style strings.
Stringstream constructor prototype is as follows:

Stringstream: stringstream (string Str );

The sample code is as follows:

# Include <iostream>
# Include <sstream>
# Include <string>
Using namespace STD;

Int main ()
{
Stringstream ostr ("CCC ");
Ostr. Put ('D ');
Ostr. Put ('E ');
Ostr <"FG ";
String gstr = ostr. STR ();
Cout <gstr <Endl;

Char;
Ostr>;
Cout <

System ("pause ");
}
In addition, the stringstream class object is also commonly used for conversion between string and various built-in data types.

The sample code is as follows:
 
# Include <iostream>
# Include <sstream>
# Include <string>
Using namespace STD;

Int main ()
{
Stringstream SSTR;
// -------- Convert int to string -----------
Int A = 100;
String STR;
SSTR <;
SSTR> STR;
Cout <STR <Endl;
// -------- Convert string to Char [] --------
SSTR. Clear (); // if you want to convert multiple types by using the same stringstream object, note that you must call clear () after each conversion ()

Member functions.
String name = "colinguan ";
Char cname [200];
SSTR <name;
SSTR> cname;
Cout <cname;
System ("pause ");
}

Next, let's learn about the input/output status signs. The input/output system in C ++ includes the results of each input/output operation.

Record information. The current status information is contained in an io_state object. Io_state is an enumeration type (just like open_mode). The following is its package

Value.

Goodbit no error

Eofbit has reached the end of the file

Failbit non-fatal input/output error, recoverable

A badbit fatal input/output error cannot be recovered

There are two ways to obtain the input/output status information. One way is to call the rdstate () function, which returns the error mark of the current status. For example, if no

If any error occurs, rdstate () returns goodbit.

The following example shows the usage of rdstate:


# Include <iostream>
Using namespace STD;

Int main ()
{
Int;
Cin>;
Cout <cin. rdstate () <Endl;
If (CIN. rdstate () = IOs: goodbit)
{
Cout <"the input data type is correct. No error! "<Endl;
}
If (CIN. rdstate () = ios_base: failbit)
{
Cout <"input data type error, non-fatal error. You can clear the input buffer to recover it! "<Endl;
}
System ("pause ");
}
Another method is to use any of the following functions to check the corresponding input/output status:

Bool bad ();

Bool EOF ();

Bool fail ();

Bool good ();

The following example shows the usage of the above member functions:


# Include <iostream>
Using namespace STD;

Int main ()
{
Int;
Cin>;
Cout <cin. rdstate () <Endl;
If (CIN. Good ())
{
Cout <"the input data type is correct. No error! "<Endl;
}
If (CIN. Fail ())
{
Cout <"input data type error, non-fatal error. You can clear the input buffer to recover it! "<Endl;
}
System ("pause ");
}
If an error occurs, the stream status is marked as an error. You must clear the error status so that your program can continue running properly. To clear the error message

The clear () function is required. This function includes a parameter, which is the flag value you want to set as the current state ., You only need to use IOs: goodbit as the real parameter.

The sample code is as follows:
 

# Include <iostream>
Using namespace STD;

Int main ()
{
Int;
Cin>;
Cout <cin. rdstate () <Endl;
Cin. Clear (IOs: goodbit );
Cout <cin. rdstate () <Endl;
System ("pause ");
}

When we find that the input is incorrect and needs to be corrected, use clear () to mark it as correct, and also use the get () member function to clear the input buffer

To achieve Repeated input.

The sample code is as follows:

# Include <iostream>
Using namespace STD;

Int main ()
{
Int;
While (1) // You can also write it as for (; 1 ;)
{
Cin>;
If (! CIN) // The condition can be changed to CIN. Fail ()
{
Cout <"incorrect input! Enter "<Endl;
Cin. Clear ();
Cin. Get ();
}
Else
{
Cout <;
Break;
}
}
System ("pause ");
}
Finally, an example of how to handle the error mark of the file stream is provided. The Code is as follows:

# Include <iostream>
# Include <fstream>
Using namespace STD;

Int main ()
{
Ifstream myfile ("C: // 1.txt", ios_base: In, 0 );
If (myfile. Fail ())
{
Cout <"failed to read the file or the specified file does not exist! "<Endl;
}
Else
{
Char ch;
While (myfile. Get (CH ))
{
Cout <ch;
}
If (myfile. EOF ())
{
Cout <"all file content has been read" <Endl;
}
While (myfile. Get (CH ))
{
Cout <ch;
}
}
System ("pause ");
}

Related Article

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.