如果程序员需要重用文件流读写多个文件,必须在读另一个文件之前用clear清除该流的状态
#include "stdafx.h"
#include
#include
using std::vector;
using namespace std;
void process(string){
}
int _tmain(int argc, _TCHAR* argv[])
{
string s;
vector files;
ifstream input;
vector::const_iterator it=files.begin();
//for each file in the vector
while(it!=files.end()){
input.open(it->c_str());//open the file
//if the file is ok,read and "process"the input
if(!input)
break;
while(input>>s){
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
return 0;
}
本文介绍了一种在C++中使用文件流处理多个文件的方法。为了确保文件流可以在不同的文件间正确复用,每次处理完一个文件后需要调用clear()方法重置流状态。示例代码展示了如何遍历文件列表并逐个打开处理。
1091

被折叠的 条评论
为什么被折叠?



