Thread: question about feof

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    63

    question about feof

    if we have this code for files
    Code:
    while(!feof(from);
      ch=fgetc(from);
      if(ferror(from){
          printf("error reading source file\n");
          exit(1);
          }
       if(!feof(from)) fputc(ch,to)/*IN THIS line why we check again with feof? */
        if(ferror(to){
          printf("error writing destination file\n");
          exit(1);
          }
     }
    my question is on the code:why we check again with if and feof?( we are in the while loop so we have already !feof(from) )

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't use feof() to control loops, see the FAQ. Other than the logic being broken, you've got a few syntax errors

    What you want is probably,
    Code:
    int c;
    
    while((c = fgetc(from)) != EOF)
    {
        fputc(c, to);
    }
    Last edited by zacs7; 01-22-2008 at 02:37 AM.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    yes but i must use feof (it is for an execise and says that i must use feof)

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by alzar View Post
    yes but i must use feof (it is for an execise and says that i must use feof)
    Use it to check if the read failed due to error or end of file, but not to control loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The reason the second feof() is needed in your code is due to the end of file marker not being set until after the fgetc() call. If you didn't have the second feof() you could process your ch variable even when fgetc() didn't return anything due to the end of the file being reached.

    If you must call feof(), you can rearange your code to be something like:

    Code:
    while(1)
    {
       read from file
       if(feof())
          break;
       process what was read from the file
    }
    Keep in mind that what zacs posted is a cleaner solution though.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    11
    yeah....instead of using feof it is better to use
    Code:
    while( (ch=fgetc(from))!=EOF)
    this even avoids the over head of calling the function everytime...
    cheers

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Quote Originally Posted by bithub View Post
    If you didn't have the second feof() you could process your ch variable even when fgetc() didn't return anything due to the end of the file being reached.
    How can this happen?
    we are in a while loop while(!feof(my_file)) and this means that the End of file not reached
    ???

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    the flag of end of file is set during read that failed, not before
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, if you consider an input stream of "abc<eof>", with the following code:
    Code:
    int ch;
    while(!feof(file)) {
       ch = fgetc(file);
       printf("%d", ch);
    }
    It will produce:
    97
    98
    99
    -1
    The loop will not end until AFTER you have read beyond the end of the file, and if you then don't check feof() AFTER the read, you will "process" the end of file input - not so bad here, but consider a fgets() where the string is not overwritten because the input was at end of file - now you may have a full string of text that was the previous last line of data, processed again!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Thanks i understand it!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM