c ++ EOF运行太多次? [英] c++ EOF running one too many times?

查看:226
本文介绍了c ++ EOF运行太多次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用EOF和/或文件,我有一个问题,我的代码挂起,我相信是因为我的EOF循环太多次。

This is my first time using EOF and/or files, and I am having an issue where my code hangs, which I believe is because my EOF is looping one too many times.

我从文件中输入,并以这种方式动态创建对象,并且在文件运行后挂起。

I am imputing from a file, and dynamically creating objects that way, and it hangs once the file is run through.

        while( !studentFile.eof() )
    {
        cout << "38\n";
        Student * temp = new Student();
        (*temp).input( studentFile );

        (*sdb).insert( (*temp) );           
    }

这段代码是有问题的代码。 cout >>38\\\
;

This chunk of code is the code in question. The cout >> "38\n"; is the line number and the reason I believe it is hanging from looping one too many times.

该文件只包含4名学生的数据,而38则出现了5次,这是我相信它循环了太多次的原因;一旦它得到最后一位数据,它似乎没有注册文件已经结束,并重新循环,但没有数据输入,所以我的代码挂起。

The file only contains 4 student's worth of data, yet 38 appears 5 times, which is the reason I believe it is looping one too many times; Once it gets the last bit of data, it doesn't seem to register that the file has ended, and loops in again, but there is no data to input so my code hangs.

如何解决这个问题?我的逻辑是否正确?

How do I fix this? Is my logic correct?

谢谢。

推荐答案

已经指出了你注意到的问题的细节。

Others have already pointed out the details of the problem you've noticed.

然而,你应该意识到,还有更多的问题,你还没有注意到。一个是相当明显的内存泄漏。每个循环执行: Student * temp = new Student(); ,但是你从不执行匹配 delete

You should realize, however, that there are more problems you haven't noticed yet. One is a fairly obvious memory leak. Every iteration of the loop executes: Student * temp = new Student();, but you never execute a matching delete.

C ++使得内存管理比一些其他语言(例如Java)更简单,需要您 new 你使用的每个对象。在C ++中,你只需要定义一个对象并使用它:

C++ makes memory management much simpler than some other languages (e.g., Java), which require you to new every object you use. In C++, you can just define an object and use it:

Student temp;

temp.input(studentFile);

这简化了代码并消除了内存泄漏 - Student 对象将在每次迭代结束时自动销毁,并且在下一次迭代开始时创建一个(概念上)新的/不同的对象。

This simplifies the code and eliminates the memory leak -- your Student object will be automatically destroyed at the end of each iteration, and a (conceptually) new/different one created at the beginning of the next iteration.

虽然它不是一个真正的错误本身,,即使这仍可以简化相当多。由于 sdb 点显然有一个 insert 成员函数,你可以像标准容器一样使用它实际上 ,虽然它并不重要)。要整理代码,首先为学生编写抽取运算符:

Although it's not really a bug per se, even that can still be simplified quite a bit. Since whatever sdb points at apparently has an insert member function, you can use it like a standard container (which it may actually be, though it doesn't really matter either way). To neaten up the code, start by writing an extraction operator for a Student:

std::istream &operator>>(std::istream &is, Student &s) {
    s.input(is);
    return is;
}

然后,您只需将数据从流复制到集合:

Then you can just copy data from the stream to the collection:

std::copy(std::istream_iterator<Student>(studentFile),
          std::istream_iterator<Student>(),
          std::inserter(*sdf));

请注意,这会自动正确处理EOF,因此您不必处理像您这样的问题一开始(即使你想要),也不容易)。

Note that this automates correct handling of EOF, so you don't have to deal with problems like you started with at all (even if you wanted to cause them, it wouldn't be easy).

这篇关于c ++ EOF运行太多次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆