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 >