Chapter 8 StandardsIoLibrary
Istream,Ostream,CIN,Cout,Cerr,>,<,
GetlineFunction.IstreamAndStringTwo reference parameters of the type.
8.1Object-oriented standard library
IoStandard library type and header file
Iostream
Fstream
Sstream
IoObjects cannot be copied or assigned values.
8.2Condition status
1. IoCondition status inside the Standard Library:
2. STRM: iostateByIostreamClass Definition, used to define the condition status
STRM: badbit STRM: iostateIndicates the destroyed stream.
STRM: failbit STRM: iostateIndicates the failed stream.
STRM: eofbit STRM: iostateType value, used to indicate that the stream has reached the end of the file
2,IoStandard Library condition status acquisition and modification functions
S. EOF ()If a stream is setSOfEofbitValue. This function returnsTrue
S. Bad ()If a stream is setSOfBadbitValue. This function returnsTrue
S. Fail ()If a stream is setSOfFailbitValue. This function returnsTrue
S. Good ()If the streamSThe function returnsTrue
S. Clear ()Clear streamSAnd setTrue
S. Clear (FLAG)StreamSIs setTrue.Flag, Type isSTRM: iostate
S. setstate (FLAG)StreamS.Flag, Type isSTRM: iostate
S. rdstate ()Get streamSThe current condition. The return value type isSTRM: iostateif (CIN )//Directly check the stream status
If (CIN )//Directly check the stream status
While (CIN> word )//Detects the stream returned by the expression and indirectly checks the stream status.
Int ival;
Whle (CIN> ival ,! Cin. EOF ()){
If (CIN. Bad ())
Throw runtime_error ("Io stream upted ");
If (cinj. Fail ()){
Cerr <"Bad Data, try again .";
Cin. Clear (istream: goodbit );
Cin. Ignore (STD: numeric_limits <STD: streamsize >:: max (), '\ n ');
Continue;
}
}
//ThisProgramRead continuouslyCIN, Until the end of the file or an unrecoverable read error occurs.
8.3Manage the output buffer
1.Refresh the output buffer
Cout <"Hi! "<Ends ;//Refresh the buffer without adding any data
Cout <"Hi! "<Endl ;//Refresh the buffer and add a line break
Cout <"Hi! "<Flush ;//Refresh the buffer and add any dataNull.
2. unitbufOperator
Cout <unitbuf <"first" <"second" <nounitbuf;
Equivalent
Cout <"first" <flush <"second" <flush;
NounitbufThe operator restores the stream to normal, and is refreshed by the System-managed buffer.
Worning: If the program crashes, the buffer will not be refreshed.
8.4File Input and Output
IfstreamRead files
OfstreamWrite files
FstreamRead and write the same file
The following program creates a fileD.txtAnd read to the screen.
# Include <iostream> # include <fstream> # include <string> using namespace STD; int main () {string STR; ofstream out ("d.txt "); STR = "ABCD \ nefgh \ nabcd \ ndfgh \ n"; out <STR <Endl; ifstream in ("d.txt"); For (string STR; Getline (in, str);) cout <STR <"\ n"; return 0 ;}
Copy the fileD.txtCopyA.txt
# Include <iostream> # include <fstream> # include <string> using namespace STD; int main () {string STR; ifstream in ("d.txt "); ofstream out ("a.txt"); For (string STR; Getline (in, STR);) out <STR <"\ n "; cout <"file copied successfully"; return 0 ;}