Re: Determining EOF using fseek()?
From: Anand (anandattek_at_yahoo.co.in)
Date: 08/30/04
- Next message: Keith Thompson: "Re: Typedef structs"
- Previous message: jacob navia: "Re: Two Questions about "strlen", "strcat" and "strcpy""
- In reply to: Orion: "Determining EOF using fseek()?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Keith Thompson: "Re: Typedef structs"
- Previous message: jacob navia: "Re: Two Questions about "strlen", "strcat" and "strcpy""
- In reply to: Orion: "Determining EOF using fseek()?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|