使用 ifstream (cpp) 永远不会达到 eof
我正在从文本文件中读取文本,但从未达到 eof,这会导致 无限循环。
这是我写的
static ifstream inF;
inF.open(file,ifstream::in);
cin.rdbuf(inF.rdbuf());
while (inF.good() && !inF.eof())
{
addStudent(students);
}
if (inF.is_open())
{
inF.close();
inF.clear();
}
循环的每次迭代,我称之为 addStudents,它只处理一行。这对我来说效果很好。基本上我的弧度线的形式为 D 98 76.5 66 45(可能)12000 这是代码:
static void addStudent(vector<Student*> students)
{
char institution;
unsigned short id;
double gAverage, pGrade, salary;
cin >> institution;
switch (institution)
{
case ALICE:
cin >> id >> gAverage >> salary;
students.push_back(new Student(institution,id,gAverage,salary));
return;
case BOB:
cin >> id >> gAverage >> salary;
students.push_back(new Student(institution,id,gAverage,salary));
return;
case COLIN:
cin >> id >> gAverage >> pGrade >> salary;
students.push_back(new CollegeStudent(institution,id,gAverage,pGrade,salary));
return;
case DANNY:
cin >> id >> gAverage >> pGrade >> salary;
students.push_back(new CollegeStudent(institution,id,gAverage,pGrade,salary));
return;
}
}
当我到达文件末尾时,循环继续运行,并且 addStudents (返回 void)不执行任何操作。有什么想法吗? 谢谢!
I am reading text from a text file, but I never reach eof, which results in an
endless loop.
Here's what I wrote
static ifstream inF;
inF.open(file,ifstream::in);
cin.rdbuf(inF.rdbuf());
while (inF.good() && !inF.eof())
{
addStudent(students);
}
if (inF.is_open())
{
inF.close();
inF.clear();
}
Every Iteration of the loop I call addStudents, which handles only one line. That works fine for me. Basically I rad lines in the form of D 98 76.5 66 45 (Possibly) 12000
here's the code:
static void addStudent(vector<Student*> students)
{
char institution;
unsigned short id;
double gAverage, pGrade, salary;
cin >> institution;
switch (institution)
{
case ALICE:
cin >> id >> gAverage >> salary;
students.push_back(new Student(institution,id,gAverage,salary));
return;
case BOB:
cin >> id >> gAverage >> salary;
students.push_back(new Student(institution,id,gAverage,salary));
return;
case COLIN:
cin >> id >> gAverage >> pGrade >> salary;
students.push_back(new CollegeStudent(institution,id,gAverage,pGrade,salary));
return;
case DANNY:
cin >> id >> gAverage >> pGrade >> salary;
students.push_back(new CollegeStudent(institution,id,gAverage,pGrade,salary));
return;
}
}
When I get to the end of the file the loop keeps running, and addStudents (which returns void) does nothing. Any Ideas why?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的文件流可能与 cin 共享其流缓冲区,但不共享其标志。因此,当您使用 cin 的
operator>>
进行读取时,它到达文件末尾,cin
设置它自己的 eof 标志,但它不会在你的ifstream
(怎么可能?它对此一无所知)。这是一种非常愚蠢的读取文件的方式,为什么要这样做呢?为什么不直接将
istream
引用传递到 addStudent 函数中并从中读取内容呢?Your file stream may share it's stream buffer with
cin
, but it doesn't share it's flags. So when you read using cin'soperator>>
, and it the reaches end of the file,cin
sets it's own eof flag, but it doesn't set the flag in yourifstream
(how could it? it has no knowledge of it).This is an awfully silly way to read a file, why are you doing it like that? Why don't you just pass an
istream
reference into your addStudent function and read from that?