关于文件传输的getline( )函数疑问
本帖最后由 IMCNS 于 2013-03-19 23:22:28 编辑
#include <iostream>
#include <string>
#include <fstream>
int main()
{
std::fstream file("ex12.txt",std::fstream::ate|std::fstream::out|std::fstream::in);
if(!file){
std::cerr<<"could not open the file"<<std::endl;
throw std::runtime_error("could not open the file");
}
std::string sentences="123 321 1234567";
file<<sentences<<std::endl;//去掉std::endl以后while里面没有数据输入
file.seekg(0,std::fstream::beg);
while (getline(file,sentences)){
std::fstream::pos_type mark=file.tellg();
file.seekp(0,std::fstream::end);
file<<sentences.size()<<" ";
//我用file.seek(mark)回到mark位置为什么循环可以正常中止?
file.seekg(mark);
}
file.close();
system ("pause");
return 0;
}