0

I wanted to know how excatly feof() works. I write some simple progrma to chek it out but results are far away from I expected. Here is a code

#include <stdio.h>
#include <stdlib.h>




int main()
{

  FILE* infile = fopen("obrazek.bmp","r");

  int a;

  while (feof(infile) != 0)
  {

  fread(&a, sizeof(a),1, infile);

  //int n = feof(infile);

 printf("%i",a);

  };


 fclose(infile);

 return 0;

}

Why feof() return 0 if I only opened a file? How to easily checked that i hitted end of a random file?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
susi33
  • 207
  • 1
  • 2
  • 4
  • 4
    Do you know what `feof()` returns and *why* it returns it ? And when you think you know the answer to that, read this: ["Why is while (!feof(file)) always wrong?"](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – WhozCraig Aug 06 '15 at 18:01
  • 2
    Your logic is backwards. **[feof](http://www.manpagez.com/man/3/feof/)** returns non-zero if it's EOF. – jweyrich Aug 06 '15 at 18:01
  • @WhozCraig sir , please also point to the almost-faq question. I seem to have messed up my bookmarks. can't find it – Sourav Ghosh Aug 06 '15 at 18:02
  • 2
    People usually post to ask why something is not working. It's rare that people ask why something does work. – mah Aug 06 '15 at 18:02
  • @SouravGhosh in a pinch, just throw `[c] feof wrong` in the search box. =P – WhozCraig Aug 06 '15 at 18:04
  • @jweyrich: Holy Grail! You have absolutley right :D. Thats te effect of lack of sleep . Thanks man :) – susi33 Aug 06 '15 at 18:06
  • @susi33 I *strongly* advise you follow [that link](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) and read more about it. Best of luck. – WhozCraig Aug 06 '15 at 18:07
  • 2
    If there isn't a FAQ for C questions there should be. 1) Why isn't `feof()` working? 2) What is wrong with `a = a++ + --a * a++`? – Weather Vane Aug 06 '15 at 18:08
  • Plus [another common mistake related to feof](http://c-faq.com/stdio/feof.html) - well, there's http://c-faq.com. – jweyrich Aug 06 '15 at 18:19
  • 2
    @susi33 in general, when something does not work the way you expect it to work, please read the documentation on that something before asking others for help. You might think it's faster to simply ask for help, but by taking that extra step you will develop your own abilities much further and faster. – mah Aug 06 '15 at 18:20
  • 1
    I've closed this as a duplicate of the 'Why `while (!feof(file))` is always wrong' question because the topics are exceptionally closely related. An alternative is to close it as 'Off-topic / trivial typo'. – Jonathan Leffler Aug 06 '15 at 18:28
  • @susi33: Er... Why would anyone expect `feof` to return anything other than `0` immediately after opening the file???? Your question sounds like "Why `2+2` is suddenly `4`?" There's nothing "sudden" in `2+2` being `4`, as there's nothing unexpected in `feof` returning `0` in your case. – AnT stands with Russia Aug 06 '15 at 18:31
  • @AnT: 2+2 can actually be 5 for sufficiently large values of 2. ;-) – DevSolar Aug 06 '15 at 18:34

1 Answers1

1

feof returns true (1) if you have previously attempted to read past the end of the file, and have not cleared it with clearerr. When you first open the file and haven't read anything, you haven't read past the end, so it returns 0.

This behavior is why using while(feof(infile) != 0) is almost always wrong. Instead, you should check the return value of your fread call and use that to control the loop.

Community
  • 1
  • 1
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 1
    Note that the loop condition should be: `while (fread(&a, sizeof(a),1, infile) != 0)`. – Jonathan Leffler Aug 06 '15 at 18:29
  • 1
    @JonathanLeffler - No, because that code can't tell how many bytes were read. – Andrew Henle Aug 06 '15 at 21:09
  • Well, the code that it's replacing also can't tell. In any case, given the options to `fread()`, it'll either return 1 (meaning `sizeof(a)` bytes were read) or 0 (meaning some smaller number of bytes was read, or possibly EOF or something else went wrong). So it does know whether the read was successful or not, and if it's successful, it knows exactly how many bytes were read. It's an improvement over using `feof()` because it tests the function that fails, rather than assuming it will succeed. Alternatively, you can use `size_t nrecs; while ((nrecs = fread(&a, sizeof(a), 1, infile)) != 0)`. – Jonathan Leffler Aug 06 '15 at 21:15