如何知道 C++ 中下一个字符是否为 EOF

发布于 2024-11-14 16:17:24 字数 359 浏览 5 评论 0原文

我需要知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

铁轨上的流浪者 2024-11-21 16:17:24

istream::peek() 返回常量当检测到文件结束或错误时,EOF保证等于-1)。要可靠地检查文件结尾,请执行以下操作:

int c = file.peek();
if (c == EOF) {
  if (file.eof())
    // end of file
  else
    // error
} else {
  // do something with 'c'
}

您应该知道底层操作系统原语 < code>read(2),仅当您尝试读取超过文件末尾时发出 EOF 信号。因此,当您仅读取文件中的最后一个字符时,file.eof() 将不为 true。换句话说,file.eof() 为 false 并不意味着下一次读取操作会成功。

istream::peek() returns the constant EOF (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:

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 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.

岁月染过的梦 2024-11-21 16:17:24

这应该可行:

if (file.peek(), file.eof())

但是为什么不在尝试读取有用数据后检查错误呢?

This should work:

if (file.peek(), file.eof())

But why not just check for errors after making an attempt to read useful data?

围归者 2024-11-21 16:17:24

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 be 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).

与他有关 2024-11-21 16:17:24

对于连接到键盘的流,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. :-)

栩栩如生 2024-11-21 16:17:24

通常检查文件结尾我使用:

    if(cin.fail())
    {
        // Do whatever here
    }

另一种实现方式是..

    while(!cin.fail())
    {
        // Do whatever here
    }

附加信息会很有帮助,这样我们就知道您想要做什么。

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.

眼眸 2024-11-21 16:17:24

无法判断下一个字符是否是文件结尾,尝试这样做是新 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.

早茶月光 2024-11-21 16:17:24

您没有显示您正在使用的任何代码,因此我有一些猜测。使用流时,通常不需要低级工具(例如 peek())。您可能感兴趣的是istream_iterator。这是一个示例,

  cout << "enter value";

  for(istream_iterator<double> it(cin), end; 
      it != end; ++it)
  {
     cout << "\nyou entered value " << *it;
     cout << "\nTry again ...";
  }

您还可以使用 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;

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,

  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:

  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;
怂人 2024-11-21 16:17:24

只需在 macOS 中使用此代码

if (true == file.eof())

即可在 macOS 中为我​​工作!

just use this code in macosx

if (true == file.eof())

it work for me in macosx!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文