Thread: Strange EOF happenings

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    15

    Strange EOF happenings

    For some reason, whenever the program reads 1A in hex, 26 in dec it is reading it as EOF. I have it print out the character and, sure enough, it prints -1. I don't get it. Other programs are detecting EOF just fine. Here's the culprit code:
    Code:
    new_byte = fgetc(_source);
       while(new_byte != EOF){
          decompress(new_byte, bit);
          for(i=0; i < (8/bit); i++){
             fputc(_new_pixels[i], _dest);
          }
          printf("%d: ", j);
          j++;
          new_byte = fgetc(_source);
          printf("%d\n", new_byte);
       }
    That is supposed to loop through over 8000 times, but it gets to byte number 206 and hit EOF. Here's the end of the output:

    Code:
    200: 0
    201: 0
    202: 15
    203: 255
    204: 139
    205: 47
    206: -1
    Byte number 206 is 26, not -1

    I changed byte 206 to 27, then it stopped on the next 26 later in the file.

    Any ideas?
    Thanks
    Last edited by fatinez; 09-23-2003 at 08:43 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Is new_byte an int like it should be?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    Yes sir.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Would something like this produce any clues?
    Code:
       new_byte = fgetc(_source);
       while ( new_byte != EOF ) {
    /*
          decompress(new_byte, bit);
          for(i=0; i < (8/bit); i++){
             fputc(_new_pixels[i], _dest);
          }
    */
          printf("%d: ", j);
          j++;
          new_byte = fgetc(_source);
          printf("%d\n", new_byte);
       }
       if(ferror(_source))
       {
          perror("_source");
       }
       if(feof(_source))
       {
          puts("Yup. EOF");
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    Code:
    200: 0
    201: 0
    202: 15
    203: 255
    204: 139
    205: 47
    206: -1
    Yup. EOF
    That's interesting. By the way, the source file is 8196 bytes.

    It only does that in Windows. I compile and run it on UNIX over ssh and it works fine. But it is going to be graded on a Windows machine. Maybe I'll just do it in UNIX and make a note of the problem.

    Here's the end of execution in UNIX:
    Code:
    8192: 0
    8193: 0
    8194: 0
    8195: 0
    8196: 0
    8197: -1
    Yup. EOF
    Last edited by fatinez; 09-23-2003 at 08:36 PM.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I don't seem to be all that helpful here. One more try: is _source opened in binary mode?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    HOLY CRAP!! That worked.

    Thanks a million Dave.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 09:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM