C++ 如果流没有任何原因就失败了?
我有一个3d球体列表,保存该列表时,我会循环:C++ 如果流没有任何原因就失败了?,c++,ifstream,C++,Ifstream,我有一个3d球体列表,保存该列表时,我会循环: void Facade::Save(std::ostream& fs) { fs<<x<<" "<<y<<" "<<z<<" "<<r<<" "; //save fields fs<<color[0]<<" "<<color[1]<<" "<<color[2]<&l
void Facade::Save(std::ostream& fs)
{
fs<<x<<" "<<y<<" "<<z<<" "<<r<<" "; //save fields
fs<<color[0]<<" "<<color[1]<<" "<<color[2]<<std::endl;
}
我不知道出了什么问题,但当读取最后一行时,无法读取最后一个球体的颜色分量,流在读取最后一个球体的半径后失败。我检查了球体列表文件:
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的bug吗?
顺便说一下,我正在使用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);
}
*/
}
void ballist::Load(std::istream&fs)
{
清除();
正面1;
而(!fs.fail()&&!fs.eof())
{
ball1.加载(fs,this);
增加(1);
}
/*
balls.pop_back();//这是一个解决方案,去掉最后一个
原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版原版;
*/
}
无效球列表::保存(标准::ostream&fs)
{
向量::迭代器itero=this->originalballs.begin();
while(itero!=this->originalballs.end())
{
itero->Save(fs);
itero++;
}
/*
//解决ifstream问题:无法读取最后一个球体的颜色
//添加一个虚拟项作为最后一个
itero=this->originalballs.begin();
如果(itero!=this->originalballs.end())
{
itero->Save(fs);
}
*/
}
在正确读取5个球(球体)后,我预计此操作将失败
循环的设计使得尝试读取第6个球将失败,但仍会调用Add()!!
您应该重新定义代码:
std::ifstream& Facade::Load(std::ifstream& fs, BallList* blist)
{
GLfloat c[3];
fs>>x>>y>>z>>r; // This will fail if there is no input.
// Once all 5 balls have been read
// There is only a new line character on the stream.
// Thus the above line will fail and the fail bit is now set.
fs>>c[0]>>c[1]>>c[2];
return fs; // returned so it can be tested in loop.
}
void BallList::Load(std::istream& fs)
{
Clear();
Facade ball1;
while(ball1.Load(fs, this)) // Only enter loop if the load worked
{ // Load worked if the stream is in a good state.
// Only call Add() if Load() worked.
Add(ball1);
}
}
白空间是你的朋友。我个人认为这更容易理解:
fs >> x >> y >> z >> r;
在正确读取5个球(球体)后,我预计这将失败 循环的设计使得尝试读取第6个球将失败,但仍会调用Add()!! 您应该重新定义代码:
std::ifstream& Facade::Load(std::ifstream& fs, BallList* blist)
{
GLfloat c[3];
fs>>x>>y>>z>>r; // This will fail if there is no input.
// Once all 5 balls have been read
// There is only a new line character on the stream.
// Thus the above line will fail and the fail bit is now set.
fs>>c[0]>>c[1]>>c[2];
return fs; // returned so it can be tested in loop.
}
void BallList::Load(std::istream& fs)
{
Clear();
Facade ball1;
while(ball1.Load(fs, this)) // Only enter loop if the load worked
{ // Load worked if the stream is in a good state.
// Only call Add() if Load() worked.
Add(ball1);
}
}
白空间是你的朋友。我个人认为这更容易理解:
fs >> x >> y >> z >> r;
请说明“失败”的确切含义。你指的是失败位吗?@Bart(谁建议编辑):我认为标题带双负片比不带双负片更正确。@Han:我们需要看更多的代码。您可能正在某个循环中执行此操作。你能给我们看看这个循环吗?你的球体列表文件似乎表明你把颜色写成整数而不是浮点数。我想知道将“GLfloat c[3]”更改为“int c[3]”是否能使它工作。无关:preference
fsp请说明“fails”的确切含义。你指的是失败位吗?@Bart(谁建议编辑):我认为标题带双负片比不带双负片更正确。@Han:我们需要看更多的代码。您可能正在某个循环中执行此操作。你能给我们看看这个循环吗?你的球体列表文件似乎表明你把颜色写成整数而不是浮点数。我想知道将“GLfloat c[3]”更改为“int c[3]”是否能让它工作。无关:首选fs