MOOC C ++ Notes (g) input and output streams

Input and output streams

Input and output streams associated with the operation of the class

TIM Screenshot 20191030205008.png
istream: class is used for the input stream, cin is the class of the object.
ostream: the output is a stream type, cout is the object of that class.
ifstream: it is for reading data from the file class.
ofstream: class is used to write data to the file.
iostream: is used for both input and output can for class.
fstream: both reading data from a file, and can write data to a class file.

Standard stream object

Input stream object: the device connected to the input cin and standard
output stream object: a standard output device is connected cout
cerr standard error output device connected to
clog the standard error output devices connected to
default
cerr<<"hello,world"<<endland clog<<"hello,world"<<endl
and cout<<"hello,world"<<endlas
cin corresponds to a standard input stream, for reading data from the keyboard, it may be redirected to read data from a file.
cout corresponds to the standard output stream, for output to the screen data, it may be redirected to write data to the file.
cerr error corresponds to the standard output stream, for outputting the error information to the screen.
clog stream corresponding to the standard error output for outputting an error message to the screen.
the difference is that cerr cerr and clog NA buffer output directly to the display; and information output to the clog in the first will be stored in the buffer output to the screen when the buffer is full or refresh.

Redirect

freopen function may be used to input and output redirection

#include<iostream>  
using namespace std;
int main(){
    int x,y;
    cin>>x>>y;
    freopen("test.txt","w",stdout);//将标准输出重定向到test.txt文件  
    if(y==0)
        cerr<<"error"<<endl;
    else
        cout<<x/y;
    return 0;
}
#include<iostream>  
using namespace std;
int main(){
    double f;
    int n;
    freopen("test.txt","r",stdout);//cin被改为从t.txt中读取数据  
    cin>>f>>n;
    return 0;
}

End Analyzing input stream

You can use the following method of determining the end of the input stream:
while(cin>>x){}
If the input is from a file, read the end of the file, even if the end of the input stream.
If the input from the keyboard, then the input Ctrl + Z separate line representing an input stream end.

Istream class member functions

getline function

istream&getline(char *buf,int bufSize);
BufSize-1 reads characters from the input stream into the buffer buf, or read hit '\ n' so far (which count whichever comes first).
istream&getline(char *buf,int bufSize,char delim)
Read from the input stream bufSize-1 characters into the buffer buf, or read delim character encountered so far (which count whichever comes first).
Both functions automatically read data is added at the end in buf '\ 0'. '\ n' or delim will not be read into buf, but will be removed from the input stream. If the input stream '\ n' number of characters before or delim meet or exceed a buf'Size, leads to reading errors, the result is: Although this reading has been completed, but it will be read after the failure a.
Input can be used to determine if (! Cin.getline (...)) is completed.

eof function

bool eof()
Determines whether the end of the input stream

peek function

int peek()
Returns the next character, but is not removed from the stream.

putback function

istream &putback(char c)
The character ch back into the input stream

ignore function

istream&ignore(int nCount=1,int delim=EOF);
Up nCount delete characters from the stream, the end of the encounter EOF.

Stream manipulators (output format)

Integer base stream: stream manipulators dec, oct, hex, setbase
precision floating point (precision, setprecision)
provided wide-field (setw, width)
user-defined stream manipulators
using stream manipulators
NOTE: Use stream manipulators need #include <iomanip>

Actuating the flow control base stream of integers Operator

Stream manipulators dec, oct, hex

int n=10;  
cout<<n<<endl;  
cout<<hex<<n<<"\n"//十六进制输出  
       <<dec<<n<<"\n"//十进制输出
       <<oct<<n<<endl;//八进制输出

Output:
10
A
10
12 is

Flow steering control precision floating-point operator

Precision, setPrecision
Precision is a member function call for: cout.precision(5)
setPrecision a stream manipulators, which call for: cout<<setprecision(5);// continuously output
the same function thereof.
Specifies the number of significant digits of floating-point output (non-fixed mode output)
to specify the number of significant digits after the decimal floating point output (output fixed-point mode)
pointing the way: decimal digits must appear in the back (non-site-directed manner generally refers to science counting)
provided in the output mode designated cout<<setiosflags(ios::fixed)<<setprecision(6)<<x<<endl;
setiosflags (ios :: fixed) position of the decimal point represents a fixed output mode, or use fixed operator.
resetiosflags (ios :: fixed) to cancel a fixed decimal point indicates the position of the output mode.

Set Domain wide stream manipulators

Provided wide-field (setw, width)
setw for the operator, width member function

int w=4;
char string[10];
cin.width(w);
cin>>string;
cout<<setw(5)<<endl;  

Input: 1234
Output: 123
Note: When reading CIN string will reserve a place for '\ 0', so the actual read only three characters.
Width setting validity is disposable, the width should be set before each read and output.

Other operators

showpos non-negative number to show positive sign.
noshowpos non-negative numbers do not show a positive sign.
It represents a fixed manner to the output point.
scientific output expressed in scientific notation.
setfill (ch) ch character to fill in the blanks indicate insufficient width.
If the width is not enough left represents the left-aligned.
a right-aligned right.
It indicates insufficient internal width, symbolic and numerical breakdown around the intermediate filling fill character

User-defined flow manipulators

ostream&tab(ostream &output){
    return output<<'\t';
}
cout<<"aa"<<tab<<"bb"<<endl;

Output: aa bb
reason this allows the user to customize stream manipulators, because of iostream where << is overloaded (member functions)

ostream &operator
<<(ostream &(*p)(ostream &));

The internal function calls the function pointed to by p, and * this as an argument to hex, dec, oct are functions.

Guess you like

Origin www.cnblogs.com/z-y-k/p/11767792.html