ifstream无缘无故失败?
|
我有一个3D球面的列表,当我保存列表时,我会遍历:
void Facade::Save(std::ostream& fs)
{
fs<<x<<\" \"<<y<<\" \"<<z<<\" \"<<r<<\" \"; //save fields
fs<<color[0]<<\" \"<<color[1]<<\" \"<<color[2]<<std::endl;
}
恢复列表时,我使用:
void Facade::Load(std::ifstream& fs, BallList* blist)
{
GLfloat c[3];
fs>>x>>y>>z>>r;
//fails there, why
fs>>c[0]>>c[1]>>c[2];
.....
}
我不知道出了什么问题,但是在读取最后一行时,无法读取最后一个球体的颜色分量,在读取了最后一个球体的半径后流失败。我检查了球体列表文件:
7.05008 8.99167 -7.16849 2.31024 1 0 0
3.85784 -3.93902 -1.46886 0.640751 1 0 0
9.33226 3.66375 -6.93533 2.25451 1 0 0
6.43361 1.64098 -6.17298 0.855785 1 0 0
6.34388 -0.494705 -6.88894 1.50784 1 0 0
看起来不错有人可以告诉我为什么会这样吗?这是ifstream的错误吗?
顺便说一下,我正在使用Unicode。
循环如下所示:
void BallList::Load(std::istream& fs)
{
Clear();
Facade ball1;
while(!fs.fail() && !fs.eof())
{
ball1.Load(fs, this);
Add(ball1);
}
/*
balls.pop_back(); //this is a work around, get rid of the last one
originalballs.pop_back();
*/
}
void BallList::Save(std::ostream& fs)
{
vector<Facade>::iterator itero = this->originalballs.begin();
while (itero != this->originalballs.end())
{
itero->Save(fs);
itero++;
}
/*
//work around the ifstream problem: the color of the last sphere cannot be read
//add a dummy item as the last
itero = this->originalballs.begin();
if(itero != this->originalballs.end())
{
itero->Save(fs);
}
*/
}
没有找到相关结果
已邀请:
1 个回复
呢率篓舍烫
PS。空白是您的朋友。我个人认为这更容易理解: