Re: Determining EOF using fseek()?

From: Anand (anandattek_at_yahoo.co.in)
Date: 08/30/04


Date: 30 Aug 2004 00:42:32 -0700

Orion <wong01@bigpond.net.au> wrote in message news:<rfHXc.10022$D7.6463@news-server.bigpond.net.au>...
> Hey,
>
> I was wondering if it was possible to determine if you hit 'EOF' using
> fseek? I'm using fseek to traverse through the file from start to end
> and capturing the data into a linked list structure. However, my loop
> doesn't seem to work well - it totally fumbles out actually:
>
> while ((a = fseek(fp,0,SEEK_CUR)) == 0){
> // code here
> }
>
> Its quite important for me not to disrupt the current position of the
> cursor since I rely on that to fetch the data from the text file. I
> thought that the loop would work fine since fseek only returns a
> non-zero integer on an error but unfortunately this is not the case.
> Anyone with suggestions with using fseek() or some other function?
>
> Any help would be greatly appreciated! Thanks.

Hi

    To read all characters from a text file upto EOF better u can go
for feof
function. Iam giving an example so that u can understand

   Method1:

               FILE *Fp;
               int ch;

               Fp=fopen("c:\\test.txt","rb");

               while((ch=fgetc(Fp))!=EOF)
                  printf("%c",ch);

               fclose(Fp);

   Method2:

              File *Fp;
              char ch;

              Fp=fopen("c:\\test.txt","rb");

              ch=fgetc(Fp);

              while(!feof(Fp))
              {

                 printf("%c",ch);
                 ch=fgetc(Fp);
              }

              fclose(Fp);

    The above two methods will work correctly. The reason EOF value is
FFFF.
It will go beyond the range. so normally an unsigned char cannot able
to hold the value of EOF. So u can get an logical error while reading
entire file becus
fgetc will treat EOF as character. Hope these ideas will make u
clear.

regards,

 Anand.



Relevant Pages

  • Question on EOF markers
    ... a combination of fprintf and fseek. ... loop as follows: ... fclose; clear ans fid; ... is it possible that I am somehow leaving bad EOF ...
    (comp.soft-sys.matlab)
  • Re: feof(), fseek(), fread()
    ... offset of a file and still be able to detect EOF?i.e. ... this without fseek(), that's why I posted this question. ... EOF is a macro defined in stdio.h probably as.. ... eoff = ftell; ...
    (comp.lang.c)
  • Re: reading file
    ... Does fseek() clears the EOF if I don't move beyond EOF? ... A more correct statement of your question is: ... Does fseekclear the end-of-file indicator if I don't move beyond ...
    (comp.lang.c)
  • Re: feof(), fseek(), fread()
    ... offset of a file and still be able to detect EOF?i.e. ... I still don't know how can I detect EOF while I'm always use fseek() ... The end-of-file indicator indicates that the last attempt to read from ...
    (comp.lang.c)
  • Re: feof(), fseek(), fread()
    ... offset of a file and still be able to detect EOF? ... You can do it with fseek, ... "Debugging is twice as hard as writing the code in the first place. ...
    (comp.lang.c)