C ++ input/output stream Summary

Source: Internet
Author: User

1.Ostream & operator <(int );

The <symbol is overloaded for int type output. Why is ostream reference returned?

For example:

Cout <"1122" <"2345" <endl;

When "1122" is output, cout is returned, that is, cout <"1122" is returned as cout, and then it can continue like cout <"2345" <endl; output in this way.

2.Output of arrays and pointers

Ostream can output the following pointer types:

Const signed char *

Const unsigned char *

Const char *

Void * // The numeric representation of the printed Address

Eg:

Char a [] = "sdwadwada" // The int type cannot be printed so intelligently on the char array cout <a <endl; // display: sdwadwadachar * a = "sdwadwada" cout <a <endl; // sdwadwadacacut <& a <endl; // print the address cout <(void *) a <endl; // print the address

 

3.

Std: cout. put (66.5); // B

Std: cout. put (65); //

Return 0;

4.Forced conversion

Long val = 560031841;

Cout. write (char *) & val, sizeof (long); // display data in 4 characters

Void main () {char ch; while (cin. get (ch) {cout <ch <"";}}

 

Version 3:

Char ch;

Ch = cin. get ();

While (ch! = '\ N ')

{

Cout <ch <""

Ch = cin. get ();

}

After the end of the file is reached, cin. get (void) returns the EOF value. Therefore, you can read the input as follows:

Declare the ch type as int rather than char, because the value EOF may not be represented by the char type.

The above experiment results can be obtained:

1. The cin indeed skips the blank space, and the terminator also skips into an infinite loop.

8. File Input and Output

8.1Header file: fstream

The file contains the iostream file automatically, so you do not need to display the header file containing the iostream.

Ostream is the base class of ofstream, and istream is the same. Therefore, all ostream methods can be used, including insertion operator definition, formatting method, and control operator.

Eg:

Ifstream fin (".....txt ");

Char ch;

While (fin. get (ch) // The method is the same as that of cin.

{

....

}

If (! Fin. is_open ())

{

...

}

8.2Open multiple files

Cin. clear () can also be avoided, depending on the situation.

Command Line Processing:

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.