Is !feof supposed to work?

This is a discussion on Is !feof supposed to work? within the C Programming forums, part of the General Programming Boards category; Hi, I've made a little program which gives me the frequency of every byte inside a file, but for some ...

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

    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,255
    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, 03:48 AM
  5. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 10:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21