开发者

How to know if the next character is EOF in C++

开发者 https://www.devze.com 2023-03-12 02:23 出处:网络
I\'m need to know if the next char in ifstream is the end of file. I\'m trying to do this with .peek():

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 a开发者_如何学Pythonsk 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.


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.


This should work:

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

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


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


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:

    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.


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.


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;


just use this code in macosx

if (true == file.eof())

it work for me in macosx!

0

精彩评论

暂无评论...
验证码 换一张
取 消