Thread: Is !feof supposed to work?

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Question Is !feof supposed to work?

    Hi,
    I've made a little program which gives me the frequency of every byte inside a file, but for some reason in a file which has no 0xFF (255) bytes, I still get a "phantom" one. I am using a "while (!feof(fp))" loop and reading the bytes using getc. Do I have !feof in the wrong place for it to work?

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    6
    The reason you are getting the 0xFF byte is that an EOF condition may occur after an attempt to read a byte from the file.
    You need to also add a check after the read to ensure that it actually read the byte.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Thanx!

    I did try doing that before, but I couldn't see how an unsigned char could be equal to EOF (should've been int, doh)...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  5. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 11:53 AM