In order to reuse an fstream object, it seems you have to call the clear member:
//定义流对象
fstream file;
//读文件
file.open("myfile.txt", ios::in);
. . .
//stream流对象不在对myfile文件进行关联
file.close();
//恢复默认的流状态
file.clear();
//写文件
file.open("myfile.txt", ios::out);
. . .
//关闭文件
file.close();