如何知道下一个字符是否是 C++ 中的 EOF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6283632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to know if the next character is EOF in C++
提问by Tae
I'm need to know if the next char in ifstream
is the end of file. I'm trying to do this with .peek()
:
我需要知道下一个字符ifstream
是否是文件的结尾。我正在尝试这样做.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
.
编辑:我想要做的是在文件中的每个单词的末尾添加一个字母。为了做到这一点,我询问下一个字符是否是标点符号,但这样最后一个单词就没有多余的字母了。我只是与char
,而不是string
。
回答by zwol
istream::peek()
returns the constant EOF
(which is notguaranteed to be equal to -1) when it detects end-of-file or error. To check robustly for end-of-file, do this:
istream::peek()
当它检测到文件结束或错误时返回常量EOF
(不保证等于 -1)。要可靠地检查文件结尾,请执行以下操作:
int c = file.peek();
if (c == EOF) {
if (file.eof())
// end of file
else
// error
} else {
// do something with 'c'
}
You should know that the underlying OS primitive, read(2)
, only signals EOF when you try to read pastthe end of the file. Therefore, file.eof()
will not be true when you have merely read up tothe last character in the file. In other words, file.eof()
being false does not mean the next read operation will succeed.
你应该知道,底层操作系统原始的,read(2)
只有当你尝试读取信号EOF过去的文件的末尾。因此,file.eof()
当你仅仅阅读不会是真的达到文件中的最后一个字符。换句话说,file.eof()
为假并不意味着下一次读取操作会成功。
回答by Ben Voigt
This should work:
这应该有效:
if (file.peek(), file.eof())
But why not just check for errors after making an attempt to read useful data?
但是为什么不在尝试读取有用数据后检查错误呢?
回答by littleadv
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 be true
.
file.eof()
返回一个标志值。如果您无法再从文件中读取,则将其设置为 TRUE。EOF 不是一个实际的字符,它是操作系统的标记。所以当你在那里 -file.eof()
应该是true
。
So, instead of if (file.peek() == file.eof())
you should have if (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).
所以,而不是if (file.peek() == file.eof())
你应该if (true == file.eof())
在阅读(或偷看)后检查你是否到达文件末尾(如果我理解正确,这就是你想要做的)。
回答by Bo Persson
For a stream connected to the keyboard the eof condition is that I intend to type Ctrl+D/Ctrl+Z during the next input.
对于连接到键盘的流,eof 条件是我打算在下一次输入期间键入 Ctrl+D/Ctrl+Z。
peek()
is totally unable to see that. :-)
peek()
完全看不到。:-)
回答by Bo Persson
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.
无法判断下一个字符是否是文件结尾,而尝试这样做是新 C 和 C++ 程序员最常犯的错误之一,因为在大多数操作系统中都没有文件结尾字符. 您可以判断的是,越过流中的当前位置读取将越过文件末尾,但这通常是非常无用的信息。相反,您应该测试所有读取操作的成功或失败,并根据该状态采取行动。
回答by tf.rz
Usually to check end of file I used:
通常检查我使用的文件结尾:
if(cin.fail())
{
// Do whatever here
}
Another such way to implement that would be..
另一种实现方式是..
while(!cin.fail())
{
// Do whatever here
}
Additional information would be helpful so we know what you want to do.
附加信息将很有帮助,因此我们知道您想做什么。
回答by Gene Bushuyev
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 is istream_iterator
. Here is an example,
你没有显示任何你正在使用的代码,所以我有一些猜测。peek()
在处理流时,您通常不需要低级工具(如)。您可能感兴趣的是istream_iterator
. 这是一个例子,
cout << "enter value";
for(istream_iterator<double> it(cin), end;
it != end; ++it)
{
cout << "\nyou entered value " << *it;
cout << "\nTry again ...";
}
You can also use istreambuf_iterator to work on buffer directly:
您还可以使用 istreambuf_iterator 直接处理缓冲区:
cout << "Please, enter your name: ";
string name;
for(istreambuf_iterator<char> it(cin.rdbuf()), end;
it != end && *it != '\n'; ++it)
{
name += *it;
}
cout << "\nyour name is " << name;
回答by lslboy
just use this code in macosx
只需在 macosx 中使用此代码
if (true == file.eof())
it work for me in macosx!
它在 macosx 中对我有用!