Thread: FEOF issues

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What if you read the file correctly with binary file I/O functions?

  2. #17
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    The idea is that you may not always be reading a text file - So you can't rely on what you have just read to determine whether you have read is the end of the file
    Fact - Beethoven wrote his first symphony in C

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm starting to get annoyed with this argument. There is nothing that says EOF directly relates to how feof() works. When fgetc() does a read, it will ignore the high order bits, anyway.

  4. #19
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by whiteflags View Post
    What if you read the file correctly with binary file I/O functions?
    This is from C File I/O Tutorial - Cprogramming.com

    When fread is used, after being passed an array, fread will read from the file until it has filled the array, and it will return the number of elements actually read. If the file, for example, is only 30 bytes, but you try to read 100 bytes, it will return that it read 30 bytes. To check to ensure the end of file was reached, use the feof function, which accepts a FILE pointer and returns true if the end of the file has been reached.
    Fact - Beethoven wrote his first symphony in C

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That quote agrees with me if anything. If the content of a file can break feof() then your implementation is broken.

  6. #21
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    ... The content of a file can't break feof() - The quote (to me) says that when using fread, where is a risk that you may miss some data at the end of the file without knowing it unless you check to see if the end of the file was reached with the feof() function. How did you get "can break feof()" from that?

    But just to clarify, we are both agree that there is a place for feof(), which is what oogabooga asked for?
    Fact - Beethoven wrote his first symphony in C

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Click_here
    ... The content of a file can't break feof() - The quote (to me) says that when using fread, where is a risk that you may miss some data at the end of the file without knowing it unless you check to see if the end of the file was reached with the feof() function. How did you get "can break feof()" from that?
    How did you get "can break feof()" from that? whiteflags certainly did not conclude that that part of the standard says that feof is broken, but it is you who used it to support your claim that:
    Quote Originally Posted by Click_here
    Testing the returned value for EOF is not always the best way of determining when you have arrived at the end of a file - Think of the case of when you are reading a binary file and one of the characters is the EOF character.
    Therefore, it is you who are claiming that feof is broken for use with binary files.

    The thing is, you are right to say that testing for EOF when reading a binary file is incorrect, but that is because using those functions that could return EOF to signal end of file is incorrect when working with a binary file. The problem isn't the test for EOF; the problem is using the wrong function, hence whiteflags countered that the solution is to read the file correctly by using binary file I/O functions, in which case feof (not testing for EOF) can be used as needed.

    EDIT:
    Wait, actually, even if you are reading a binary file that contains a character with a byte that matches whatever the value of EOF is on the system, you should still be safe since EOF is negative and the per-character input functions only return unsigned char as ints... well, except in the very special case where the range of int is the same as the range of signed char, and the stars align in the conversion from unsigned char to int.
    Last edited by laserlight; 09-30-2012 at 09:20 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I got "can break feof()" from whiteflags post #20 "If the content of a file can break feof() then your implementation is broken". I think that I misread that.


    I was initially agreeing with whiteflags and refering to oogabooga's post #11 and his use of getchar == EOF asking them to consider a binary file input


    I stand by my claim that I didn't say that feof() would break from a files input, because I do not believe that - I couldn't find where someone could get that from the things that I posted.
    Fact - Beethoven wrote his first symphony in C

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I was initially agreeing with whiteflags and refering to oogabooga's post #11 and his use of getchar == EOF asking them to consider a binary file input
    Read closer, I never made such a comparison in my example code.

    Furthermore, oogabooga didn't say anything that I necessarily disagree with. His opinion is that the usual file reading idiom is better, because it will stop the loop in the case of errors. My objection to that is at least as far as my code is concerned a valid file handle is all that is necessary, and I tried to cover all my bases when I said that the file has to be locked to greatly diminish the chance of errors that ferror() will care about. But he is still absolutely correct.


    If anyone was morbidly curious, please consider the following. In glibc, feof() comes down to this bit of magic:
    Code:
    866#ifdef IO_DEBUG
    867# define CHECK_FILE(FILE, RET) \
    868	if ((FILE) == NULL) { MAYBE_SET_EINVAL; return RET; } \
    869	else { COERCE_FILE(FILE); \
    870	       if (((FILE)->_IO_file_flags & _IO_MAGIC_MASK) != _IO_MAGIC) \
    871	  { MAYBE_SET_EINVAL; return RET; }}
    Drill down for yourself here. Cross Reference: /glibc/libio/feof.c

    I apologize about the extra line numbering. (But it even says _IO_MAGIC doesn't it ) You can clearly see the internal indicater being tested. Well anyway, I'm sorry I cannot guarantee that all feof() implementations are exactly this, but it would seem against the design rationale to do any more than what is absolutely required.

    The reason that oogabooga is correct may be obvious but if you are doing anything more complicated than reading text, like formatting input, then the flags for a bad state could be set, by say, scanf(). But chances are, the function that you are using has it's own facilities to tell you something went tits up.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Click_here
    I got "can break feof()" from whiteflags post #20 "If the content of a file can break feof() then your implementation is broken". I think that I misread that.
    What whiteflags means is that if the contents of a file can cause feof to be broken -- as in report the end of file status incorrectly -- then that test input proves that the given implementation of feof is broken, i.e., your standard library implementation would have a bug that needs to be fixed.

    Quote Originally Posted by Click_here
    I stand by my claim that I didn't say that feof() would break from a files input, because I do not believe that - I couldn't find where someone could get that from the things that I posted.
    I think you did not realise that whiteflags' post #16 asked a rhetorical question. whiteflags wasn't asking how to read a binary file correctly with binary file I/O functions; whiteflags was posing a question that mimicked your own to get you to realise that there were binary file I/O functions for which your concerns about EOF with binary files don't matter at all, e.g., because the feof function might be used instead. I guess that you took the question literally, and thus quoted something that supports whiteflags' point, as if it supported your own point, but you believed that it would help answer whiteflags' question.

    Quote Originally Posted by Click_here
    I was initially agreeing with whiteflags and refering to oogabooga's post #11 and his use of getchar == EOF asking them to consider a binary file input
    Right. Do you understand now that binary file input is not a problem here?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    See comments in quote

    Quote Originally Posted by laserlight View Post
    What whiteflags means is that if the contents of a file can cause feof to be broken -- as in report the end of file status incorrectly -- then that test input proves that the given implementation of feof is broken, i.e., your standard library implementation would have a bug that needs to be fixed.


    OK, understood




    I think you did not realise that whiteflags' post #16 asked a rhetorical question.


    I'm fairly bad at them.


    whiteflags wasn't asking how to read a binary file correctly with binary file I/O functions; whiteflags was posing a question that mimicked your own to get you to realise that there were binary file I/O functions for which your concerns about EOF with binary files don't matter at all, e.g., because the feof function might be used instead. I guess that you took the question literally, and thus quoted something that supports whiteflags' point, as if it supported your own point, but you believed that it would help answer whiteflags' question.


    I felt that whiteflags was stating that feof was not needed if functions like fread are used - I found a quote that I felt contradicted that and introduced another point. Just to clarify, what do you think that I was arguing?


    Right. Do you understand now that binary file input is not a problem here?


    So there is nothing that you could put in a binary file that could cause a problem? I suppose that I can accept that
    Fact - Beethoven wrote his first symphony in C

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Click_here
    I felt that whiteflags was stating that feof was not needed if functions like fread are used - I found a quote that I felt contradicted that and introduced another point.
    Right, but whiteflags did not state that.

    Quote Originally Posted by Click_here
    Just to clarify, what do you think that I was arguing?
    I think that you were arguing that "testing the returned value for EOF is not always the best way of determining when you have arrived at the end of a file" because you thought that "if your binary file is full of integers and one of the is the EOF character", there could be a problem. One thing that I suspect you missed is that fread does not return EOF to signal end of file.

    Quote Originally Posted by Click_here
    So there is nothing that you could put in a binary file that could cause a problem? I suppose that I can accept that
    A problem for what? You need to be specific.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    A problem for what? You need to be specific.
    Let's say, getc() from a binary file. But you have already clarified that for me.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP: feof
    By dlf723 in forum C Programming
    Replies: 5
    Last Post: 07-23-2010, 08:49 AM
  2. feof() from FAQ
    By salvadoravi in forum C Programming
    Replies: 6
    Last Post: 01-25-2008, 01:08 PM
  3. cannot get out of while( !feof (f) )
    By SoFarAway in forum C Programming
    Replies: 2
    Last Post: 02-19-2005, 03:36 PM
  4. feof issues
    By Rare177 in forum C Programming
    Replies: 13
    Last Post: 11-23-2004, 10:11 AM
  5. feof ....EOF
    By GanglyLamb in forum C Programming
    Replies: 12
    Last Post: 12-04-2002, 12:38 PM