ifstream无故失败
ifstream fails without any reason?
我有一个三维球体的列表,当我保存列表时,我循环通过:
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的一个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);
}
*/
}
我预计在正确读取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);
}
}
PS。留白是你的朋友。就我个人而言,我认为这更容易阅读:
fs >> x >> y >> z >> r;
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- ifstream什么都没读
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- C++ 在使用ifstream时,在Apache下运行的CGI文件失败
- ifstream.read 在现有文件上失败,如果上一个打开失败
- 为什么ifstream在读取混合数据时失败
- ifstream.read() 设置如果读取量小于可用值则失败位
- 正在从转换失败的ifstream中获取数据
- std::ifstream::read()读取的数据少于请求的数据,并设置失败位,原因不明显
- 为什么ifstream读取0x80000000 int失败?
- 什么会导致ifstream代码在OS X上失败?
- ifstream无故失败
- ifstream 失败,因为我的文件描述符用完了