PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 31, 2022

[FIXED] Why can't I access EOF using fseek()?

 October 31, 2022     c, eof, file     No comments   

Issue

#include <stdio.h>

int main()
{
    FILE * fp = fopen("Introduce.txt","rt");

    fseek(fp,0,SEEK_END);
    int i = feof(fp);
    printf("%d",i);

    fseek(fp,1,SEEK_END);
    i = feof(fp);
    printf("%d",i);

    fseek(fp,-1,SEEK_END);
    i = feof(fp);
    printf("%d",i);

    return 0;
}

I tried to access EOF positioning 'file position indicator' at the end of the file.
But the result of this code is "000". Why does this happen?


Solution

The feof() function doesn't report that it is at EOF until you try to read some data and there is no data to read.

You can seek beyond the current EOF of a file that's open for writing (or that's open for reading and writing).

See while (!feof(file)) is always wrong for more information on why you seldom if ever need to use feof(). In some ways, feof() is a function you should forget about — most of your programs will improve if you assume it doesn't exist.



Answered By - Jonathan Leffler
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing