Introduction of C + + IO Class (1) stream and state __c++ of flow

Source: Internet
Author: User
Basic IO Library Type: istream (Input stream) type, provides input operation Ostream (output stream) type, provides output operation Cin, a IStream object, reads data from standard input cout, a Ostream object, writes data to standard output cerr, A Ostream object, usually used for outputting program error messages, to write to standard error &GT;&GT: Used to read input data from a IStream object <&lt: To write data to a Ostream object getline: Reads data from a given IStream and deposits it in a string object
File IO class: Applications often need to read and write named files, so we can read and write data ifstream from files Ofstream write data to file FStream read and write files through FStream
String IO class: Istringstream reads data from a string Ostringstream writes data to a string
Ifstream and Istringstream are both inherited from IStream, so use the same method
Io object has no object or copy:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main () {

	ofstream out1, Out2;
	OUT1 = Out2; The stream object cannot be assigned a value
	ofstream print (ofstream);  Cannot initialize ofstream parameter
	out2 = print (OUT2);    Cannot copy Stream object


	system ("PAUSE");
	return 0;
}

Functions for IO Operations generally use the reference method, read and write an IO object will change its state pass and return of the reference can not be a const
Detect stream input is correct: read operation sometimes error: such as int ival; Cin>> ival; If you enter boo, an error occurs that does not correctly read the data, so to detect
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

void Testistreamright () {
	int ival;
	if (CIN >> ival)
		cout << "right" << Endl;
	else
		cout << "wrong" << Endl;
}
int main () {

	testistreamright ();
	
	System ("PAUSE");
	return 0;
}

The state of the query stream: Sometimes we also need to know why the IO input output has an error, which requires flow state detection and then chooses the correct way to handle it.
The IO Library defines a machine-independent iostate type that provides the full functionality of the expression flow state Badbit indicates a system-level error: If unrecoverable or read-write error, once placed, it can no longer be restored Failbit: After a recoverable error occurs, the Failbit is reset and the error can be fixedAfter the end of a file read: Eofbit and failbit will be placed, goodbit value is, indicating that no error badbit,failbit,eofbit any one of the bits, indicating an error occurred

Management condition Status: Rdstate (); Remember, CIN. The current state SetState function will give the position of the condition, indicating that the corresponding error occurred
Clear reset corresponding operation bit;
Detect whether individual bits have been changed:
void Checkbit () {
	if (cin.eof ()) {
		cout << "EOF change!" << Endl;
	}

	if (Cin.fail ()) {
		cout << "fail change!" << Endl;
	}

	if (Cin.bad ()) {
		cout << "Bad change!" << Endl;
	}

	if (Cin.good ()) {
		cout << "Good change" << Endl
	}
}

The specific application of the bit state:
int main () {
	Auto old_state = Cin.rdstate ();  Remember the current state of Cin
	Cin.clear ();   All bit resets make the operation effective
	
	testistreamright ();  Use of CIN
	checkbit ();

	Cin.setstate (old_state);  Revert to initial State

	//manual reset
	cout << endl << "after SetState:" << Endl;

	Cin.clear (Cin.rdstate () & ~cin.failbit & ~cin.badbit);  The failbit bit and badbit bit are restored checkbit () by manual reset ~ expression bit reverse
	.

	System ("PAUSE");
	return 0;
}






















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.