" INFILE"对象无法读取EOF [英] "inFile" object cannot read EOF

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

问题描述

您好。我正在研究C ++ Primer Plus这本书。作者:Stephan Prata。在

第6章中,他提供了一个从文件中读取的练习。名单是

因此:


4

Sam Stone

2000

Freida Flass

100500

Tammy Tubbs

5000

Rich Raptor

55000


所以我创建了一个istream对象inFile,并将该文件与

关联起来。我知道它读取文件,因为我已经在每次读取指令后放置了cout指令

。 (自从取出)。但我无法获得

程序来阅读EOF。


在以下代码之后有条件可以测试

inFile.eof()和inFile.fail()但这些被跳过,到

默认else条件。


有谁能看出为什么EOF不被识别?

inFile> size;

捐赠者* pd = new捐赠者[尺寸];

而(inFile.get()!=''\ n'')

;


for(int i = 0; i< size; i ++){

getline(inFile,pd [i] .name); //这里有一个cout stmt和

inFile> pd [i] .cash; //这里让我知道数据是

读取

while(inFile.get()!=''\ n'')

;

}

Hello. I''m studying the book "C++ Primer Plus" by Stephan Prata. In
chapter 6 he gives an exercise that reads from a file. The list is
thus:

4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000

So I create a istream object, inFile, and associate the file with
it. I know it reads the file because I''ve placed cout directives
after every read instruction. (since taken out). But I can''t get the
program to read EOF.

After the following code there are conditions that test for
inFile.eof() and inFile.fail() but these get skipped over, to a
default "else" condition.

Can anyone see why EOF is not being recognized?
inFile >size;
donor *pd = new donor[size];
while(inFile.get() != ''\n'')
;

for(int i = 0; i < size; i++){
getline(inFile, pd[i].name); //a cout stmt here and
inFile >pd[i].cash; //here lets me know the data was
read
while(inFile.get() != ''\n'')
;
}

推荐答案

waltbrad写道:
waltbrad wrote:

您好。我正在研究C ++ Primer Plus这本书。作者:Stephan Prata。在

第6章中,他提供了一个从文件中读取的练习。名单是

因此:


4

Sam Stone

2000

Freida Flass

100500

Tammy Tubbs

5000

Rich Raptor

55000


所以我创建了一个istream对象inFile,并将该文件与

关联起来。我知道它读取文件,因为我已经在每次读取指令后放置了cout指令

。 (自从取出)。但我无法获得

程序来阅读EOF。
Hello. I''m studying the book "C++ Primer Plus" by Stephan Prata. In
chapter 6 he gives an exercise that reads from a file. The list is
thus:

4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000

So I create a istream object, inFile, and associate the file with
it. I know it reads the file because I''ve placed cout directives
after every read instruction. (since taken out). But I can''t get the
program to read EOF.



阅读EOF是什么意思?什么是EOF在你的陈述中?

What do you mean by "to read EOF"? What''s "EOF" in your statement?


在以下代码之后有条件来测试

inFile.eof()和inFile.fail()但是这些被跳过,到

默认其他条件。
After the following code there are conditions that test for
inFile.eof() and inFile.fail() but these get skipped over, to a
default "else" condition.



这在FAQ 5.8中有所说明。

This is covered in the FAQ 5.8.


有谁能看出为什么EOF没被识别?
Can anyone see why EOF is not being recognized?



每个读取操作都以某种终结符结束。例如,

读取一个数字(''pd [i] .cash'')终止于一个符号,该符号不能成为一个数字的一​​部分(如换行符) 。当文件光标在文件末尾_at_时,你很可能永远不会尝试读取
,以强制设置

''eof''位。既然你告诉你的程序确切地读了多少

次,它就没有读不懂,这就是为什么''失败'的原因是

也没有设定。尝试删除文件中最后一个

编号后的换行符 - 将55000中的第三个0设置为文件中的_last_符号

。您应该看到差异。

Every read operation ends with some kind of terminator. For example,
reading a number (''pd[i].cash'') terminates at a symbol that cannot
be part of a number (like a newline). Most likely you never attempt
to read when the file cursor is _at_ the end of the file, to force the
''eof'' bit to be set. Since you tell your program exactly how many
times to read, it doesn''t fail to read, and that''s why ''fail'' bit is
not getting set either. Try removing the newline after the last
number in your file - make the third ''0'' in ''55000'' the _last_ symbol
in the file. You should see the difference.


>


inFile> size;

捐助者* pd =新捐赠者[size];

while(inFile.get()!=''\ n'')

;

for(int i = 0; i< size; i ++){

getline(inFile,pd [i] .name); //这里有一个cout stmt和

inFile> pd [i] .cash; //这里让我知道数据是

读取

while(inFile.get()!=''\ n'')

;

}
>

inFile >size;
donor *pd = new donor[size];
while(inFile.get() != ''\n'')
;

for(int i = 0; i < size; i++){
getline(inFile, pd[i].name); //a cout stmt here and
inFile >pd[i].cash; //here lets me know the data was
read
while(inFile.get() != ''\n'')
;
}



V

-

请删除资金' 'A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


6月7日下午2:58,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Jun 7, 2:58 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

>

这包含在FAQ 5.8中。
>
This is covered in the FAQ 5.8.



再次问好。谢谢你的回复。实际上我到处都是你的回答

。但是,你证实了我的怀疑,我不知道测试它们的方式




我不知道有一个这个新闻组的常见问题解答。我在哪里可以找到它?


再次感谢。

Hello again. Thanks for the response. Actually I was getting around to
your answer. But, you confirmed my suspicions, and I didn''t know a way
to test them.

I waasn''t aware that there was a faq for this newsgroup. Where can I
find it?

Thanks again.


waltbrad写道:
waltbrad wrote:

>

我不知道这个新闻组有一个常见问题。我在哪里可以找到它?

>
I waasn''t aware that there was a faq for this newsgroup. Where can I
find it?

http://www.parashift.com / c ++ - faq-lite /

DN

-

[没有x''在我的电子邮件中]


我有权保持沉默

(并且应该尽可能多地使用它)

我输入的任何内容都可以并将用于对付我

在白痴法庭中

我有权利出错

(可能是我)

如果我不能提出自己的错误

我相信有人会为我提供它。

http://www.parashift.com/c++-faq-lite/

DN
--
[there are no x''s in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I''m sure someone will provide it for me.


这篇关于&QUOT; INFILE&QUOT;对象无法读取EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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