Thread: How do I check to see if an ifstream object has reached end of file?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question How do I check to see if an ifstream object has reached end of file?

    How do I check to see if an ifstream object has reached the end of file?
    I expected there to be an "atEnd()" function or something, but no such thing exists in the ifstream class, it seems, so how does one check to see if its reached the end of the file yet?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can use eof().

    However, normally input loops are controlled with the input statement itself. The eof bit may not be set until only after an attempt to read more input has failed.

    Code:
    while (getline(fin, line))
    
    while (fin >> word)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Thanks.
    I think eof should work fine in my case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Replies: 8
    Last Post: 12-06-2008, 02:43 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM