EOF问题 [英] EOF problem

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

问题描述

我有一个函数,可以将文本文件中的记录列表读入链表。但如果文件不存在,则假设创建一个。我尝试了一段时间(!file.eof()),但即使是已经存在的或新创建的空文件,它似乎仍然会在循环中运行一次。通过循环运行它会使我的链表填充垃圾值,并在尝试访问链表时导致后来崩溃。

展开 | 选择 | Wrap | 行号

解决方案


我有一个函数,可以将文本文件中的记录列表读入链表。但如果文件不存在,则假设创建一个。我尝试了一段时间(!file.eof()),但即使是已经存在的或新创建的空文件,它似乎仍然会在循环中运行一次。通过循环运行它会使我的链表填充垃圾值,并在尝试访问链表时导致后来崩溃。

展开 | 选择 | Wrap | 行号

我无法使用ios :: in和ios:app打开模式创建一个不存在的文件。我正在使用gnu 3.4.5。以下链接( http://rafb.net/efnet_cpp/tutorials/ .. ._openmode.html )说gnu正在使用正确的行为。您应该只能使用trunc创建一个新文件。我真的不知道哪个编译器是正确的。


你可以尝试另一个选项:ios :: ate。这会在创建时将指针设置为文件的末尾。这可能适用于您的系统。我怀疑eof没有设置用ios:app创建文件,因为ios :: app意味着用于写作,而不是阅读。在设置eof之前,您可能需要进行读取。也许ios :: ate open会解决这个问题。


为什么要打开具有读/写权限的数据文件?通常,数据文件是只读的,并且具有写入权限打开它是一个等待发生的错误。


AdrianH:


它已被删除一段时间以来我做了c ++编程,我不记得如何重载运算符。这只是一个家庭作业(我已经超出了要求),但如果我发现这个问题,我可能会尝试回去清理一些代码(就像你建议的那样)。我也会将代码全部发布,以便你可以编译它。


RRick:


我会尝试一些你建议的不同方法。我不担心程序的安全性(就像我说这只是一个家庭作业),但是拥有ios :: in和ios:app的原因是因为我不知道如何创建一个文件,如果它不存在。



这里是整个程序的代码:

** *代码转移到下一篇文章,因为它太长了***


如果文件已经存在并且至少存有一个帐户,它似乎工作正常。但由于没有存储的数据,程序崩溃了。如果你想制作一个文件进行测试,只需称它为banks.txt即可。并在其中放入一条记录:


500 Mary Jane 789.79

确保在789.79之后没有换行符

I have a function that is suppose to read a list of records from a text file into a linked list. But if the file doesn''t exist it is suppose to create one. I tried a while (!file.eof()) but it still seems to run through the loop once even if is an already existing or newly created empty file. By running through the loop it is filling my linked list with garbage values and results in later crashing when trying to access the linked list.

Expand|Select|Wrap|Line Numbers

解决方案

I have a function that is suppose to read a list of records from a text file into a linked list. But if the file doesn''t exist it is suppose to create one. I tried a while (!file.eof()) but it still seems to run through the loop once even if is an already existing or newly created empty file. By running through the loop it is filling my linked list with garbage values and results in later crashing when trying to access the linked list.

Expand|Select|Wrap|Line Numbers


I can not get this code to create a non-existent file with ios::in and ios:app open modes. I''m using gnu 3.4.5. The following link (http://rafb.net/efnet_cpp/tutorials/..._openmode.html) says that gnu is using the correct behavior. You should be able to create a new file only with trunc. I really don''t know which compiler is right.

There is another option you can try: ios::ate. This sets the pointer to the end of the file at creation. This might work with your system. I suspect that eof is not set on creation of the file with ios:app because ios::app is meant to be used with writing, not reading. You might need to do a read before eof is set. Maybe the ios::ate open will fix this.

Why are you opening a data file with read/write permissions? Generally, data files are read only and opening them with write permission is a bug waiting to happen.


AdrianH:

It''s been a while since I did c++ programming and I couldn''t remember how to do overloaded operators. This is just a homework assignment (one where I have already gone way beyond what was required) but if I get this problem figured out, I will might try to go back and clean up the code some (like what you suggested). I will also post the code in it''s entirety so you can compile it.

RRick:

I will try some of the different methods you suggested. I''m not worries about the security of the program (like I said this is just a homework assignment), but the reason for having ios::in and ios:app is because I didn''t know how else to create a file if it didn''t exist.



Here''s the code for the entire program:
***Code moved to next post becuase it was too long***


It seems to work fine if a file already exist and has at least one account stored in it. But with no stored data the program crashes. If you want to make a file to test it with, just call it "banks.txt" and put one record in it like this:

500 Mary Jane 789.79

Make sure there is no newline after "789.79"


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

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