如何知道 C++ 中下一个字符是否为 EOF
我需要知道 ifstream 中的下一个字符是否是文件末尾。我正在尝试使用 .peek()
来做到这一点:
if (file.peek() == -1)
但
if (file.peek() == file.eof())
两者都不起作用。有办法做到这一点吗?
编辑:我想做的是在文件中每个单词的末尾添加一个字母。为了做到这一点,我询问下一个字符是否是标点符号,但这样最后一个单词就没有多余的字母了。我只使用 char
,而不是 string
。
I'm need to know if the next char in ifstream
is the end of file. I'm trying to do this with .peek()
:
if (file.peek() == -1)
and
if (file.peek() == file.eof())
But neither works. There's a way to do this?
Edit: What I'm trying to do is to add a letter to the end of each word in a file. In order to do so I ask if the next char is a punctuation mark, but in this way the last word is left without an extra letter. I'm working just with char
, not string
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
istream::peek()
返回常量当检测到文件结束或错误时,EOF
(不保证等于-1)。要可靠地检查文件结尾,请执行以下操作:您应该知道底层操作系统原语 < code>read(2),仅当您尝试读取超过文件末尾时发出 EOF 信号。因此,当您仅读取文件中的最后一个字符时,
file.eof()
将不为 true。换句话说,file.eof() 为 false 并不意味着下一次读取操作会成功。istream::peek()
returns the constantEOF
(which is not guaranteed to be equal to -1) when it detects end-of-file or error. To check robustly for end-of-file, do this:You should know that the underlying OS primitive,
read(2)
, only signals EOF when you try to read past the end of the file. Therefore,file.eof()
will not be true when you have merely read up to the last character in the file. In other words,file.eof()
being false does not mean the next read operation will succeed.这应该可行:
但是为什么不在尝试读取有用数据后检查错误呢?
This should work:
But why not just check for errors after making an attempt to read useful data?
file.eof()
返回一个标志值。如果您无法再读取文件,则将其设置为 TRUE。 EOF 不是一个实际的字符,它是操作系统的一个标记。因此,当您在那里时 -file.eof()
应该为true
。因此,您应该在读取后使用
if (true == file.eof())
而不是if (file.peek() == file.eof())
(或查看)检查您是否到达文件末尾(如果我理解正确的话,这就是您想要做的)。file.eof()
returns a flag value. It is set to TRUE if you can no longer read from file. EOF is not an actual character, it's a marker for the OS. So when you're there -file.eof()
should betrue
.So, instead of
if (file.peek() == file.eof())
you should haveif (true == file.eof())
after a read (or peek) to check if you reached the end of file (which is what you're trying to do, if I understand correctly).对于连接到键盘的流,eof 条件是我打算在下一次输入期间键入 Ctrl+D/Ctrl+Z。
peek()
完全看不到这一点。 :-)For a stream connected to the keyboard the eof condition is that I intend to type Ctrl+D/Ctrl+Z during the next input.
peek()
is totally unable to see that. :-)通常检查文件结尾我使用:
另一种实现方式是..
附加信息会很有帮助,这样我们就知道您想要做什么。
Usually to check end of file I used:
Another such way to implement that would be..
Additional information would be helpful so we know what you want to do.
无法判断下一个字符是否是文件结尾,尝试这样做是新 C 和 C++ 程序员最常见的错误之一,因为大多数操作系统中没有文件结尾字符。您可以看出,读取流中的当前位置将读取文件末尾,但这通常是非常无用的信息。您应该测试所有读取操作是否成功或失败,并根据该状态采取行动。
There is no way of telling if the next character is the end of the file, and trying to do so is one of the commonest errors that new C and C++ programmers make, because there is no end-of-file character in most operating systems. What you can tell is that reading past the current position in a stream will read past the end of file, but this is in general pretty useless information. You should instead test all read operations for success or failure, and act on that status.
您没有显示您正在使用的任何代码,因此我有一些猜测。使用流时,通常不需要低级工具(例如
peek()
)。您可能感兴趣的是istream_iterator
。这是一个示例,您还可以使用 istreambuf_iterator 直接处理缓冲区:
You didn't show any code you are working with, so there is some guessing on my part. You don't usually need low level facilities (like
peek()
) when working with streams. What you probably interested in isistream_iterator
. Here is an example,You can also use istreambuf_iterator to work on buffer directly:
只需在 macOS 中使用此代码
即可在 macOS 中为我工作!
just use this code in macosx
it work for me in macosx!