首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

ifstream 的有关问题

2013-03-25 
ifstream 的问题//------------------写入------------------------vectorstring testtest.push_back(

ifstream 的问题
//------------------写入------------------------
vector<string> test;
test.push_back("111");
test.push_back("222");

ofstream out("test.dat",ios_base::out | ios_base::binary);
copy(test.begin(),test.end(),ostream_iterator<string>(out,"\n"));
int i = 10;
out.write((char*)(&i) , sizeof(int));//这个写入的10,怎么在下面读取出来呢?
out.close();
test.clear();
//------------------读取-------------------------
ifstream read("test.dat",ios_base::in | ios_base::binary);
int pos = read.tellg(); //pos = 0 
copy(istream_iterator<string>(read),istream_iterator<string>(),back_inserter(result));
if(read.fail())
{
// 进来了
}
pos = read.tellg(); //pos = -1

//------------------ 问题-------------------------
我用了 copy(istream_iterator<string>(read),istream_iterator<string>(),back_inserter(result));
之后,文件流被关闭了,我怎么才能读取 写入的 i ?
难道非要我遍历vector,一个一个自己写,再一个一个自己读?
[解决办法]
最好遍历vector,一个一个自己写,再一个一个自己读。
[解决办法]
同意楼上的。

热点排行