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

Source: Internet
Author: User

1 opening and closing of files

1.1 Defining a Stream object

Ifsteam iflie;//file input Stream object

Ifsteam iflie;//file output stream object

Fsteam iflie;//file input/output stream object

1.2 Open File

void Open (const unsigned char* filename,int mode,int accessfilebuf:opnprot)

Mode is the open way, the relevant tag as shown in (iOS)

    

Attention:

(1) usually open way with | combine together

Ios::in|ios::out//Read-write mode open file

Ios::out|ios::binary//binary write mode open file

(2) Iftream: Default Ios::in

Ofstream: Default Ios::out|ios::trunc

FStream: Default Ios::in|ios::out|ios::app

2 State functions

EOF (): Read file reaches end true

Bad (): Error reading file returns True when I open a write that cannot be written

Fail: We enter a letter when we need to enter the shaping

Good: Returns False if any of the above functions are true

Clear ():

#include <iostream>#include<stdlib.h>using namespacestd;intMain () {intIntegerv; cout<<"before a bad input operation:"<<"\ cin.eof ():"<<cin.eof ()<<"\ Cin.fail ():"<<Cin.fail ()<<"\ncin.bad ():"<<Cin.bad ()<<"\ncin.good ()"<< Cin.good () <<Endl; CIN>> Integerv;//Control +d/zcout <<"After a bad input operation:"<<"\ cin.eof ():"<<cin.eof ()<<"\ Cin.fail ():"<<Cin.fail ()<<"\ncin.bad ():"<<Cin.bad ()<<"\ncin.good ()"<< Cin.good () <<Endl; System ("Pause"); return 1;}

3 (CIN) and (! CIN) Analysis

Whether the while (CIN) or if (CIN) is legal why? Customize a class and then define the object of the class, using the IF statement to determine that it is illegal. Indicates that the stream object has a convert function to convert a stream object into a type that the judgment statement can recognize

operator void* () const function in while (CIN) or if (CIN) implicit conversion bit void* type
BOOL operator! () const; The function is called in while (!cin) or if (!cin) to convert the stream object to a bool object
while (CIN)--->while (!cin.fail)
while (!cin)---->while (Cin.fail)

1 classA2 {3  Public:4 A () {}5~A () {}6     operator void*()Const7     {8cout <<"cast to void*";9         return(void*) This;Ten     } One     BOOL operator! ()Const A     { -cout <<"cast to bool"; -         return true; the     } - }; -  - intMain () + { - A; +     if(a) cout <<" First"<<Endl; A     if(!a) cout <<"Second"<<Endl; atSystem"Pause"); -     return 1; -}

4 file read and write operations

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

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.