返回值ifstream.peek()当它到达文件的结尾 [英] Return value of ifstream.peek() when it reaches the end of the file

查看:994
本文介绍了返回值ifstream.peek()当它到达文件的结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看这篇关于Cplusplus.com的文章, http:// www。 cplusplus.com/reference/iostream/istream/peek/



我仍然不确定peek()如果到达文件的结尾就返回。



在我的代码中,只要此语句为真,程序的一部分应该运行

 (sourcefile.peek()!= EOF)

sourcefile是我的ifstream。



但是,它永远不会停止循环,即使它已经到达文件的末尾。



EOF不是指「档案结束」吗?

>

返回: traits :: eof() if good() is false 。否则,返回 rdbuf() - > sgetc()


sgetc()


返回:如果输入序列读取位置不是可用,返回 underflow()


下溢,


如果暂挂序列为null,函数将返回 traits ::


所以yep,返回 EOF 在文件末尾。



一个更简单的方法是返回 int_type 。由于 int_type 的值只是 char_type 加上EOF的值,它可能返回 char_type



如其他人所说, peek 提前文件位置。通常最简单的方法是最好的,最好只是循环 while(input_stream),并让失败获得额外的输入杀死解析过程。


I was looking at this article on Cplusplus.com, http://www.cplusplus.com/reference/iostream/istream/peek/

I'm still not sure what peek() returns if it reaches the end of the file.

In my code, a part of the program is supposed to run as long as this statement is true

(sourcefile.peek() != EOF)

where sourcefile is my ifstream.

However, it never stops looping, even though it has reached the end of the file.

Does EOF not mean "End of File"? Or was I using it wrong?

解决方案

Consulting the Standard,

Returns:traits::eof() ifgood()isfalse. Otherwise,returnsrdbuf()->sgetc().

As for sgetc(),

Returns: If the input sequence read position is not available, returns underflow().

And underflow,

If the pending sequence is null then the function returns traits::eof() to indicate failure.

So yep, returns EOF on end of file.

An easier way to tell is that it returns int_type. Since the values of int_type are just those of char_type plus EOF, it would probably return char_type if EOF weren't possible.

As others mentioned, peek doesn't advance the file position. It's generally easiest and best to just loop on while ( input_stream ) and let failure to obtain additional input kill the parsing process.

这篇关于返回值ifstream.peek()当它到达文件的结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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