使用 ifstream (cpp) 永远不会达到 eof

发布于 2024-12-03 01:58:37 字数 1327 浏览 0 评论 0原文

我正在从文本文件中读取文本,但从未达到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒儿 2024-12-10 01:58:37

您的文件流可能与 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's operator>>, and it the reaches end of the file, cin sets it's own eof flag, but it doesn't set the flag in your ifstream(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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文