- 1 //files是一个vector对象,包含一些要打开并读取的文件名
- 2 //每次循环构造一个名为input的ifstream的对象
- 3 while(it != files.end()){
- 4 ifstream input(it->c_str());
- 5 if(!input) break;
- 6 while(input >> s) process(s);
- 7 ++it;
- 8 }
- 9 //也可将input定义移到while外,那么需要更仔细地管理流对象。
- 10 //每次需要打开新文件,故要关闭当前的文件流
- 11 //关闭流不能改变流的内部状态,如果读写操作失败,状态将保持为错误模式,故需要调用clear
- 12 ifstream input;
- 13 vector<string>::const_iterator it = files.begin();
- 14 while(it != files.end()){
- 15 input.open(it->c_str());
- 16 if(!input) break;
- 17 while(input >> s) process(s);
- 18 input.close();
- 19 input.clear();
- 20 ++it;
- 21 }
来源: