Thread: feof ....EOF

  1. #1
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110

    Question feof ....EOF

    I was just wandering what the difference between EOF and feof is.
    the only thing i know is that in my code EOF gives errors like cannot convertchar to _*char or something and feof doenst.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    feof() is a function to test the file stream to determine if it has hit end of file.

    The feof() function tests the end-of-file indicator for the stream pointed to by fp. Because this indicator is set when an input operation attempts to read past the end of the file, the feof() function detects the end of the file only after an attempt is made to read beyond the end of the file. Thus, if a file contains 10 lines, the feof() won't detect the end of the file after the tenth line is read; it will detect the end of the file once the program attempts to read more data.
    EOF is an int (usually -1) that is returned by the OS when doing reads on streams.
    Code:
    int c;
    while ((c = fgetc(fp)) != EOF)
    {
       do something
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110

    Thumbs up

    EOF int thats right the errors were cannot convert int to_*char
    i think i got it sorta ..thx hammer

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    29
    I believe feof() is used more for binary files and EOF is used more for text type files.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>EOF, <snip>, is similar to the null character ( \0 ) in a string. It just signifies the end of your file.
    Not exactly. As I said it's a value returned by various read functions to denote the end of a file. EOF doesn't exist within the data itself.

    >>feof() is used more for binary files and EOF is used more for text type files.
    In a word... no.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    29
    So hammer is one better suited for one instance and one for another? The statement I made came from Sams Teach Yourself C in 24 hours.
    "This function [feof()] is more useful when you're reading a binary filebecause the values of some bytes may be equal to the value EOF. That is to say, the character which is used as an end-of-file marker in a text file can easily occur in a binary file, but in that case it is not intended to mark end of the file. If you try to determine the end of a binary file by checking the value returned by fread(), you may end up at a wrong position. Using the feof() functions helps you to avoid mistakes in determining the end of a file, because it checks whether the file position indicator has actually reached the end of the file, regardless of any EOF present."(Zhang 367).

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Its a bit hard to comment on a book I haven't read, but based on what you've posted, here goes:
    This function [feof()] is more useful when you're reading a binary filebecause the values of some bytes may be equal to the value EOF. That is to say, the character which is used as an end-of-file marker in a text file can easily occur in a binary file, but in that case it is not intended to mark end of the file.
    Wrong. As I already said (twice was it?), EOF is an int, usually set to -1. If you read a character from a file you will get a byte (char). Unless you screw up your code, the two will never be the same. If they were, EOF would be pretty pointless. Look:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        /*
         * In char terms, -1 is 0xff.
         * In this sample, c represents the return
         * value from a call to fgetc().  It is assigned
         * -1 which is found in the fictious input file 
         * as a 0xff byte.  To prove the point, x is
         * the same, but using the 0xff notation.
         */
        int c = (unsigned char) -1;
        int x = 0xff;
        
        printf ("EOF is 0x%0x\n", EOF);
        printf ("c   is 0x%0x\n", c);
        printf ("x   is 0x%0x\n", x);
        return 0;
    }
    
    /*
    Output:
        EOF is 0xffffffff
        c   is 0xff
        z   is 0xff
    */
    If you try to determine the end of a binary file by checking the value returned by fread(), you may end up at a wrong position.
    Confusingly put. fread() doesn't return the value of what it has read, instead it returns the number of complete elements successfully read, which could be less than you requested. At this point you should use feof() and/or ferror() to determine why it failed to read what you asked it to. If you don't understand what I mean here, read up about the fread() function and all should become clear.

    Using the feof() functions helps you to avoid mistakes in determining the end of a file, because it checks whether the file position indicator has actually reached the end of the file, regardless of any EOF present."(Zhang 367).
    Well, if you still don't get it by now, here's some code to play around with. It uses fgetc() to get a char at a time from an input file and display it in hex on stdout. I suggest you create myfile.bin with your favourite hex editor and put whatever values you like in it. See if you can "break" this program.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        FILE *fp;
        int c;
        
        if ((fp = fopen("myfile.bin", "rb")) == NULL)
        {
            perror("myfile.bin");
            return EXIT_FAILURE;
        }
        
        while ((c = fgetc(fp)) != EOF)
        {
            printf ("0x%0x ", c);
        }
        putchar ('\n');
        
        fclose(fp);
        
        return 0;
    }
    A common mistake is to use a char to catch the return from fgetc(). In this case, you do have a problem, because the type conversions will make a char of -1 look the same as an int -1. To test this out, simply create a file containing only 0xff bytes, and alter the following one line of the last program I gave:
    >>int c;
    becomes
    >>char c;

    And finally for clarity:
    fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error.

    fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.

    For more details, see your manual
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    29
    By no means is this book my "manual." It has been my first book on C yes, but i hardly use the thing due to the fact that reading it bores the hell out of me. I do not need you to sit there and patronize me on a topic that i have understood from day one. I wanted for you to explain your side more as both of you had conflicting views and since some on this board are still learning (you as well as I) I wanted for them to see the difference in the two views. I mearly brought this quote in as you seemed to have some expertise on the subject and I wanted to get your reply on what the difference was between the two.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by WolfPack
    By no means is this book my "manual." It has been my first book on C yes, but i hardly use the thing due to the fact that reading it bores the hell out of me. I do not need you to sit there and patronize me on a topic that i have understood from day one.
    Obviously you didn't understand it if you were asking questions on it. You were citing a point of reference on your belief of why feof was used.

    You didn't, and don't, understand, or you wouldn't have bothered asking:
    So hammer is one better suited for one instance and one for another?
    Why ask if you know, right? Personally, I never use feof. I suppose you could find use for it, but I have yet to find a place where I need to use feof instead of EOF.

    Furthermore, depending on how you use your loop, you can end up reading past the end of the file if all you're checking is feof. This is why it's common practice around here to suggest people test for EOF rather than just using feof. Because the status that feof sets can be cleared and you'll merrily keep reading past the end of your file.

    If you would have spent a few moments actually reading, instead of feining offence, you might have learned a thing or two.

    Furthermore, I don't see how the reply was patronizing at all. Hammer speficicly gave examples of how to use EOF, and descriptions of the functions used.

    Remember, you are not the only one reading the forum. While you may refuse to be helped by the post, this doesn't mean that everyone will disregard it.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    29
    To a degree I knew the difference between the two in what code I have written. I have myself have always used EOF and when flipping through the book I saw the feof(), and read the several pages devoted to it and that is what I know or knew between the two.

    I take offense to little remarks quickly and a bit over the edge at times, though this was not one of them. Just little remarks, for instance.
    Wrong. As I already said (twice was it?),

    still don't get it by now
    I do appreciate you post Hammer. It cleared some stuff up for me and hopefully other people too.
    Quzah, at the time when I was reading his post things he had said and how he had said them hit me the wrong way and I kinda went into deffense. Sorry if my post instigated something with you too as it seems did. I am sorry Hammer for attacking you and I am sorry Quzah.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    My post wasn't meant to patronise

    >>By no means is this book my "manual."
    You do realise that I was actually giving you a link to a good reference site, which is called the manual pages?

    >>I take offense to little remarks quickly
    Well, best change that, else you're going to get real annoyed round here

    >>I suppose you could find use for it, but I have yet to find a place where I need to use feof instead of EOF.
    An example would be when using fread(), which doesn't return EOF at all.
    fread does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    next time if i have a question like this i wont forget to mention that evry opinion is a start for me because i dont know a thing about the thing im asking so....
    because off the binary thing with EOF i began to think and actually its "good" in a way that ppl might post "wrong" things
    thx anyway
    Last edited by GanglyLamb; 12-04-2002 at 06:00 AM.

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    29
    Yea I picked up on that manual site after I posted, hehe. Good link though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgetc and EOF
    By pic-o-matic in forum C Programming
    Replies: 18
    Last Post: 08-29-2008, 09:08 AM
  2. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 09:09 PM
  3. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM