Overview of IO input and output streams in C + + < a >

Source: Internet
Author: User

1 IO class diagram relationships

1.1 Simplified form

1.1.2 Supplement

iostream

IStream: Reading from the stream

Ostream: Write to stream

Iosteram: Read and Write Streams

FStream:

Ifstream: Read from File

Ofstream: Writing Files

FStream: Read and write files

2 Stream Overview

2.1 What can cause brush buffering

(1) The program ends normally, and all buffers are emptied as part of the end of the main function

(2) Buffer full brush buffer

(3) Endl Flush Brush Buffer

(4) After each output operation, set the flow internal state with the UNITBUF operator to empty the buffer

2.2 Standard output

Output Stream object: Cout,cerr,clog

2.2.1 Formatted output Iomanip

(1) The relevant control characters and functions as shown

(2) control the output with the format control (remember to include the header file Iomanip Oh, here too)

1 intMain ()2 {3     4     intn = -;5cout <<"To set the system in:"<<Endl;6cout <<"decimal"<< N <<Endl;7cout <<"hexadecimal"<< hex << N <<Endl;8cout <<"octal"<< Oct << N <<Endl;9cout <<"decimal"<< Dec << N <<Endl;TenCin.Get(); One     return 1; A}

(3) Set field width:::: SETW (n) n less than actual width output according to actual width

1 intMain ()2 {3     intm =1234;4cout <<"Set the field width"<<Endl;5cout << SETW (3) << m << Endl;//12346cout << SETW (5) << m << Endl;//12347cout << SETW (Ten) << m << Endl;//12348Cin.Get();9 Ten     return 1; One}

(4) Set the fill character setfil ..... Need to be used with SETW

1 intmain6 ()2 {3     intx =1234;4cout <<"set the fill character"<<Endl;5cout << Setfill ('*') &LT;&LT;SETW (5) <<x<< Endl;//*12346cout << SETW (Ten) <<x<< Endl;//******12347     8Cin.Get();9     return 1;Ten}

(5) Set alignment Setiosflags (ios::left/right)

1 intmain7 ()2 {3     inty =1234;4cout <<"Set Alignment"<<Endl;5cout << Setfill (' ');6cout << setiosflags (ios::left) << SETW (Ten) << y <<Endl;7cout << setiosflags (ios::right) << SETW (Ten) << y <<Endl;8 9Cin.Get();Ten     return 1; One  A}

(6) Show decimal and positive negative sign

1 intMain8 ()2 {3     DoubleD1 =Ten/5, D2 =22.0/7;4cout <<"display the decimal digits and symbols"<<Endl;5cout << d1 << Endl;//26     //2.000007cout << setiosflags (ios::showpoint) << D1 <<Endl;8     //+3.14286 Force display symbols9cout << setiosflags (ios::showpos) << D2 <<Endl;Ten     //+3.14286 Onecout << D2 <<Endl; ACin.Get(); -     return 1; -  the}

(7) Setting progress

1 intmain10 ()2 {3     DoubleDD =123.4567;4     //1.2e+0025cout << Setprecision (2) << DD <<Endl;6     //1237cout << Setprecision (3) << DD <<Endl;8     //123.59cout << Setprecision (4) << DD <<Endl;Ten     //123.46 will be rounded up here Onecout << Setprecision (5) << DD <<Endl; ACin.Get(); -     return 1; -  the}

3 standard input CIN

3.1 member function get

Char get () enter a character and return (enter tab, etc.)

istream& get (char &)//Implement chained programming

 1  int   Main ()  2  { 3  char   ch;< /span>4  while  (Cin.get   5   { 6   cout.put (CH); 7  }  8  return  1   9 } 

istream& get (Char*,int,char)//read N-1 characters from input stream, assign to character array

Parameter: Character array character count terminating character

Note: The contents of the char* point will be emptied if it is not read to n-1 characters or the Terminator will block

3.2 Getline

istream& getline (Char*,int,char)

Unlike get, the terminating character is terminated before the n-1 character is read. The maximum difference is get when encountering a delimiter stop at that time Bu ' hu

Cin.getline (buf,1024, ' G ');

Overview of IO input and output streams in C + + < a >

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.