FILE *ptr;
char i;
ptr=fopen("file.c","r");
while((i=fgetc(ptr))!=Null)
printf("%c",i);
leads to infinite loop???
but
while(fgets(str,80,ptr)!=NULL)
puts(str);
on the similiar terms doesn't???
FILE *ptr;
char i;
ptr=fopen("file.c","r");
while((i=fgetc(ptr))!=Null)
printf("%c",i);
leads to infinite loop???
but
while(fgets(str,80,ptr)!=NULL)
puts(str);
on the similiar terms doesn't???
read the manual! fgetc() does not return NULL when end-of-file. It returns EOF.
FILE *ptr;
char i;
ptr=fopen("file.c","r");
while((i=fgetc(ptr))!=Null)
printf("%c",i);
leads to infinite loop???but
while(fgets(str,80,ptr)!=NULL)
puts(str);on the similiar terms doesn't???
Maybe because fgetc ()
returns EOF when end of file is reached or an error is encountered hence your loop never terminates while fgets ( )
returns NULL whenever it encounters End of file or an error.
So maybe something like while ((i = fgetc (ptr) != EOF)
should do the job.
PS. It is generally not advisable to base your file reading loop on EOF since it has the prob of reading an extra record and then jumping out of the loop.
HOpe it helped, bye.
> while((i=fgetc(ptr))!=Null)
As well as the EOF thing, i should be an int, not a char.
It needs to be capable of storing all 256 possible values of char AND EOF (257 different values in total).
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.