《C++ Primer》 第08章 标准 IO 库 | Good !

第08章:标准IO库
——C++的输入/输出(input/output)由标准库提供。标准库定义了一族类型,支持对文件和控制窗口等设备的读写(IO)。
第一节:面向对象的标准库
@ 学习摘录062:iostream定义读写控制窗口的类型
——istream 从流中读取
——ostream 写到流中去
——iostream 对流进行读写;从istream和ostream派生而来
@ 学习摘录063:fstream定义读写已命名文件的类型
——ifstream 从文件中读取;由istream派生而来
——ofstream 写入到文件中;由ostream派生而来
——fstream读写文件;由iostream派生而来
@ 学习摘录064:sstream定义的类型用于读写存储在内存中的string对象
——istringstream  从string对象中读取;由istream派生而来
——ostringstream  写入到string对象中去;由ostream派生而来
——stringstream  对string对象进行读写,由iostream派生而来
第二节:条件状态(condition state)
@ 学习摘录065:IO错误例子:
——以下例子,如果在标准输入设备输入Borges。
——则cin在尝试将输入的字符串读为int型数据失败后,会生成一个错误状态。
——如果输入文件结束符(end-of-file)。
——则cin也会进入错误状态。
——而如果输入1024,则成功读取,cin将处于正确的无错误状态。
——流必须处于无错误状态,才能用于输入或输出。
——检测流是否可用的最简单的方法是检查其真值。

if(cin) // ok to use cin, it is in a valid state
while(cin >> word) // ok: read operation successful…
@ 学习摘录066:各种条件状态的定义
——s.bad(),badbit标志着系统级的故障,如无法恢复的读写错误。
——s.fail(),failbit标志着出现可恢复的错误,这种导致设置failbit的问题通常是可以修正的。
——s.eof(),eofbit遇到文件结束符时设置的。
——s.good(),如果bad、fail或者eof中的任意一个为true,则检查流本身将显示该流处于错误状态。如果这三个条件没有一个为true,则good操作将返回true。
——s.clear(),clear操作将条件重设为有效状态。
@ 学习摘录067:流状态的查询和控制
——回顾逗号操作符的求解过程:首先计算它的每一个操作数,然后返回值右边的操作数作为整个操作的结果。
int ival;
// read cin and test only for EOF; loop is executed even if there are other IO failures
while(cin >> ival, !cin.eof() )  // 哦!了解吧!
{
  if (cin.bad())                   // input stream is corrupted; bail out 
      throw runtime_error(“IO stream corrupted”);
  if (cin.fail())                   // bad input
  {
cerr << “bad data, try again”;  // warn the user
cin.clear(istream::failbit);     // reset the stream
continue;                  // get next input
   }
  // ok to process ival
}

第三节:输出缓冲区的管理
@ 学习摘录068:缓冲区的刷新
——下面五种情况将导致缓冲区的内容被刷新,即写入到真实的输出设备或者文件:
——1. 程序正常结束。作为main返回工作的一部分,将清空所有的输出缓冲区。
——2. 在一些不确定的时候,缓冲区可能已经满了,在这种情况下,缓冲区将会写到下一个值之前刷新。
——3. 用操纵符(manipulator)显式地刷新缓冲区,例如行结束符endl.
——4. 在每次输出操作执行完后,用unitbuf操纵符设置流的内部状态,从而清空缓冲区。
——5. 可将输出流与输入流关联(tie)起来。在这种情况下,在读输入流将刷新其关联的输出缓冲区。
@ 学习摘录069:unitbuf操纵符与flush操纵符   Good !
——如果需要刷新所有输出,最好使用unitbuf操纵符。
——unitbuf操纵符在每次执行完写操作后都刷新流:
——cout << unitbuf << “first” << “second” << nounitbuf;
——等价于 cout << “first” << flush << “second” << flush;
第四节:文件的输入与输出
@ 学习摘录070:读取一个存放文件名的容器,打开每个文件
——此例中,如果忽略clear的调用,则循环只能读入第一个文件。 Good !
ifstream input; 
vector<string>::const_iterator it = files. begin();
// for each file in the vector
while( it != files.end() ) 
{
  input.open(it -> c_str());  // open the file
  // if (!input)
     break;             // error: bail out!
while(input >> s)     // do the work on this file
   process(s);
input.close();        // close file when we’re done with it
input.clear();        // reset state to ok
++it;               // increment iterator to get next file
}
第五节:字符串流
@ 学习摘录071:操纵每行中的每个单词的实例
string line, word;              // will hold a line and word from input, respectively
while(getline(cin, line))         // read a line from the input into line
{  // do per-line processing
 
 istringstream stream(line);   // bind to stream to the line we read
   while(stream >> word)      // read a word from line

   {
     // do per-word processing
   }
}
——使用getline函数从输入读取整行内容。然后为了获得每行中的单词,将一个istringstream对象与所读取的行绑定起来,这样只需使用普通的string输入操作符即可读出每行中的单词。
原文地址:https://www.cnblogs.com/robbychan/p/3787182.html