The function eof() returns true if the end of the associated input file has been reached, false otherwise.
For example, the following code reads data from an input stream in and writes it to an output stream out, using eof() at the end to check if an error occurred:
流的状态由bad、fail、eof和good操作揭示。clear和setstate操作用于改变条件成员状态
int ival;
//read cin and test only for EOF;loopis executed even if there are other IO failures
while( cin>>ival,cin.eof() ) //先读取,然后返回是否到达文件结束
{
if( cin.bad() )
{
throw runtime_error("IO stream corrupted");
}
if( cin.fail() )
{
cerr<< "bad data,try again";
cin.clear(istream::failbit); //reset the stream
continue;
}
}
输入缓冲区的刷新:
cout<< "hi"<<flush; //flush the buffer;adds no data
cout<< "hi"<<ends; //insert a null,then flushes the buffer
cout<< "hi"<<endl; //insert a newline,then flushes the buffer
cout<< unitbuf<< "fisrt"<< "second"<< nounitbuf;//每次执行完写操作符后都刷新流