A
Andy
I read the C.FAQ
http://c-faq.com/stdio/feof.html
I know it is not a good practice to use feof(). My question is: if I
have the following code,
--------------------------------------------------------<code>
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fp;
char line[50];
char *p;
fp = fopen("tmp", "r"); /* fp == NULL check is ignored */
printf("fpos=%d\n", ftell(fp));
while(!feof(fp)){
p = fgets(line, sizeof line, fp);
printf("p=%p, %s", p, line);
printf("fpos=%d\n", ftell(fp));
}
fclose(fp);
exit(0);
}
--------------------------------------------------------<code>
--------------------------------------------------------<tmp>
line1\n
line2\n
line3\n
--------------------------------------------------------<tmp>
why the last line twice?
After opening the file, fpos = ftell(fp) is 0, then fgets() reads
"line1", so fpos=6.
fgets() reads "line2"
fpos = 12
fgets() reads "line3"
fpos = 18
right now, fp is pointing at the end of the file, while is a place
after "line3\n". The next time fgets() trys to read again, it will
signal the end-of-file, and also the line is SUPPOSED to be set to
'\0', so it should NOT print the previous store line value again.....
Here is an implementation of K&R2 on p.165 of fgets()
--------------------------------------------------------<code>
char *fgets(char *s, int n, FILE *iop){
register int c;
register char *cs;
cs = s;
while(--n >0 && (c=getc(iop)) != EOF)
if((*cs++ = c) == '\n') break;
*cs = '\0';
return (c==EOF && cs == s)? NULL : s;
}
--------------------------------------------------------<code>
If I use this function, then the last line will not appear TWICE... I
am thinking how the stdlib really implements this function...
BTW, fscanf() will signal the END_OF_FILE IMMEDIATELY after its
read????
Thanks for any advice!
~Andy W.
http://c-faq.com/stdio/feof.html
I know it is not a good practice to use feof(). My question is: if I
have the following code,
--------------------------------------------------------<code>
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fp;
char line[50];
char *p;
fp = fopen("tmp", "r"); /* fp == NULL check is ignored */
printf("fpos=%d\n", ftell(fp));
while(!feof(fp)){
p = fgets(line, sizeof line, fp);
printf("p=%p, %s", p, line);
printf("fpos=%d\n", ftell(fp));
}
fclose(fp);
exit(0);
}
--------------------------------------------------------<code>
--------------------------------------------------------<tmp>
line1\n
line2\n
line3\n
--------------------------------------------------------<tmp>
why the last line twice?
After opening the file, fpos = ftell(fp) is 0, then fgets() reads
"line1", so fpos=6.
fgets() reads "line2"
fpos = 12
fgets() reads "line3"
fpos = 18
right now, fp is pointing at the end of the file, while is a place
after "line3\n". The next time fgets() trys to read again, it will
signal the end-of-file, and also the line is SUPPOSED to be set to
'\0', so it should NOT print the previous store line value again.....
Here is an implementation of K&R2 on p.165 of fgets()
--------------------------------------------------------<code>
char *fgets(char *s, int n, FILE *iop){
register int c;
register char *cs;
cs = s;
while(--n >0 && (c=getc(iop)) != EOF)
if((*cs++ = c) == '\n') break;
*cs = '\0';
return (c==EOF && cs == s)? NULL : s;
}
--------------------------------------------------------<code>
If I use this function, then the last line will not appear TWICE... I
am thinking how the stdlib really implements this function...
BTW, fscanf() will signal the END_OF_FILE IMMEDIATELY after its
read????
Thanks for any advice!
~Andy W.