标准IO库条件状态clear的使用

先看下面一段代码:

 1     ifstream input; 
 2     vector<string>::const_iterator it = files.begin(); 
 3     //   for each file in the vector 
 4     while (it != files.end()) { 
 5         input.open(it->c_str());  // open the file 
 6         // if the file is ok, read and "process" the input 
 7         if (!input) 
 8             break;                    // error: bail out! 
 9         while(input >> s) // do the work on this file 
10             process(s); 
11         input.close();        // close file when we're done with it 
12         input.clear();        // reset state to ok 
13         ++it;                 // increment iterator to get next file 
14     } 

第12行“input.clear();”很容易被忽略(经测试:clear在close之前和之后调用结果都正确),因为感觉上文件流被open并close之后已经一切正常,但实际上,流的状态已经发生变化,所以,必须使用clear才能继续使用。下面是《C++ Primer》的相关解释:

“  如果忽略 clear 的调用,则循环只能读入第一个文件。要了解其原因,就需要考虑在循环中发生了什么:首先打开指定的文件。假设打开成功,则读取文件直到文件结束或者出现其他错误条件为止。 在这个点上, input 处于错误状态。如果在关闭(close)该流前没有调用 clear 清除流的状态,接着在 input 上做的任何输入运算都会失败。一旦关闭该文件,再打开 下一个文件时,在内层 while 循环上读 input 仍然会失败——毕竟最后一次对流的读操作到达了文件结束符,事实上该文件结束符对应的是另一个与本文件无关的其他文件。 
  如果程序员需要重用文件流读写多个文件,必须在读另一个文件之前调用 clear 清除该流的状态。”

原文地址:https://www.cnblogs.com/tingshuixuan2012/p/2998034.html