C ++ primer Study Notes (chapter 8 standard Io Library)

Source: Internet
Author: User

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

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.