C + + Sstream standard library Detailed introduction of _c language

Source: Internet
Author: User

Introduction of C + + Sstream standard library
Next we continue to look at the C + + style of streaming control, C + + introduced the Ostringstream, Istringstream, StringStream these three classes, to use them to create objects must include sstream.h header files.

The Istringstream class is used to perform input operations on C + +-style streaming.
The Ostringstream class is used to perform the output operation of a C-style streaming stream.
The Strstream class can also support the C-style streaming input and output operation.

The Istringstream class derives from the IStream (input stream Class) and Stringstreambase (c + + string stream base class), Ostringstream from ostream (output stream Class) and Stringstreambase (c+ + String Stream base class), StringStream is derived from the iostream (input output stream Class) and Stringstreambase (c + + string stream base class).

Their inheritance relationship is shown in the following illustration:

Istringstream is constructed from a string object, and the Istringstream class reads characters from a string object.

The Istringstream constructor is as follows:
Istringstream::istringstream (string str);

Copy Code code as follows:

#include <iostream >
#include <sstream >
using namespace Std;
int main ()
{
Istringstream ISTR;
Istr.str ("1 56.7",);
The above two processes can be simply written Istringstream istr ("1 56.7");
cout << istr.str () <<endl;
int A;
float B;
istr>>a;
cout <<a<<endl;
istr>>b;
cout <<b<<endl;
System ("pause");
}

In the example above, when constructing a string stream, the space becomes the inner boundary of the string parameter, which is proved by the input "assignment" operation of the A,b object in the example, and the space of the string becomes the decomposition point of the integer data and the floating-point data. Using the method of boundary acquisition we actually complete the split conversion process of the string to the integer object and the floating-point object.

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

Ostringstream is also constructed from a string object, and the Ostringstream class inserts characters into a string.

The Ostringstream constructor is as follows:
Ostringstream::ostringstream (string str);

The sample code is as follows:

Copy Code code as follows:

#include <iostream >
#include <sstream >
#include <string >
using namespace Std;
int main ()
{
Ostringstream Ostr;
OSTR.STR ("abc"); If the string parameter is set at the time of construction, then the growth operation does not increase from the end, but instead modifies the original data, which exceeds the portion growth
Ostr.put (' d ');
Ostr.put (' e ');
ostr<< "FG";

String gstr = Ostr.str ();
cout <<gstr;
System ("pause");
}

In the example code, we can continue to insert a single character or a string into the ostr by using the put () or the left-shift operator, which returns the full string data after the growth through the STR () function, but it is noteworthy that when the string data is already in the object when it is constructed, Then the growth operation does not increase from the end, but instead modifies the original data and exceeds the portion growth.

For StringStream, needless to say, you already know that it is for C + + style string input and output.

The StringStream constructor is as follows:
Stringstream::stringstream (string str);

The sample code is as follows:

Copy Code code 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 A;
ostr>>a;
cout <<a

System ("pause");
}

In addition, the objects of the StringStream class are often used for converting between string and various built-in type data.

The sample code is as follows:

Copy Code code as follows:

#include <iostream >
#include <sstream >
#include <string >
using namespace Std;

int main ()
{
StringStream Sstr;
--------int turns to string-----------
int a=100;
String str;
sstr<<a;
sstr>>str;
cout <<str<<endl;
--------String char[]--------
Sstr.clear ()//If you want to implement multiple types of conversions by using the same StringStream object, be aware that the clear () member function must be called after each conversion.
String name = "Colinguan";
Char cname[200];
sstr<<name;
sstr>>cname;
cout <<cname;
System ("pause");
}

Next, we will learn about the status of the input/output of the relevant knowledge, C + + responsible for the input/output system includes the results of each input/output operation of the record information. These current state information is contained in an object of type Io_state. Io_state is an enumeration type (just like Open_mode), and here is the value it contains.

Goodbit No errors
Eofbit has reached the end of the file
Failbit non fatal input/output error, can be redeemed
Badbit fatal input/output error, irreversible

There are two ways to get the status information for the input/output. One way is by calling the Rdstate () function, which returns the error token for the current state. For example, if there are no errors, rdstate () returns GOODBIT.

The following example shows the use of Rdstate ():

Copy Code code as follows:

#include <iostream >
using namespace Std;

int main ()
{
int A;
CIN >>a;
cout <<cin. Rdstate () <<endl;
if (CIN. rdstate () = = iOS:: goodbit)
{
cout << "The type of input data is correct, no error!" "<<endl;
}
if (CIN. rdstate () = ios_base::failbit)
{
cout << "input data type error, non-fatal error, can clear the input buffer to save!" "<<endl;
}
System ("pause");
}

Another method is to use any of the following functions to detect the corresponding input/output status:

bool Bad ();
BOOL EOF ();
bool Fail ();
bool Good ();

The following example shows the use of each member function above:

Copy Code code as follows:

#include <iostream >
using namespace Std;

int main ()
{
int A;
CIN >>a;
cout <<cin. Rdstate () <<endl;
if (cin. Good ())
{
cout << "The type of input data is correct, no error!" "<<endl;
}
if (CIN. FAIL ())
{
cout << "input data type error, non-fatal error, can clear the input buffer to save!" "<<endl;
}
System ("pause");
}

If the error occurs, then the flow state is marked as an error, and you must clear the state of the error so that your program continues to run correctly and properly. To clear the error state, you need to use the clean () function. This function takes a parameter, which is the flag value you want to set as the current state. , as long as the ios::goodbit is used as an argument.

The sample code is as follows:

Copy Code code as follows:

#include <iostream >
using namespace Std;

int main ()
{
int A;
CIN >>a;
cout <<cin. Rdstate () <<endl;
Cin. Clear (iOS:: goodbit);
cout <<cin. Rdstate () <<endl;
System ("pause");
}

Usually when we find that the input is wrong and need to be corrected, using clear () change is marked as correct, and you need to use the Get () member function to purge the input buffer to achieve the purpose of duplicate input.

The sample code is as follows:

Copy Code code as follows:

#include <iostream >
using namespace Std;

int main ()
{
int A;
while (1)
{
CIN >>a;
if (!cin)//condition can be rewritten as cin.fail ()
{
cout << "input is wrong!" Please re-enter "<<endl;
Cin. Clear ();
Cin. Get ();
}
Else
{
cout <<a;
break;
}
}
System ("pause");
}

Finally, give an example of file flow error tag processing, consolidate learning, the code is as follows:
Copy Code code as follows:

#include <iostream >
#include <fstream >
using namespace Std;

int main ()
{
Ifstream myfile ("C://1.txt", ios_base::in,0);
if (Myfile.fail ())
{
cout << "file read failed or the specified file does not exist!" <<endl;
}
Else
{
Char ch;
while (Myfile.get (CH))
{
cout <<ch;
}
if (myfile.eof ())
{
cout << "The contents of the file have all been read" <<endl;
}
while (Myfile.get (CH))
{
cout <<ch;
}
}
System ("pause");
}

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.