Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EOF problem

Status
Not open for further replies.

ty1975

Programmer
Nov 5, 2002
12
GB
I have a file, which I need to check constantly for new data. Let's say if last time I read the data until the end of the file. Then one minute later, new data has been written this file, so I start to use fgetc to new data from last the file position. But every time it shows empty, unless I increament two positions. I do not understand why?

Here is part of my code:

do {
....................

fseek(out, position, SEEK_SET); // position is the global variable for storing file pointer position
....................
get_line(buffer1, out);
while(strlen(buffer1) > 0)
{
if (strlen(buffer1) > 18)
{
sprintf(buffer2, "%s\n", &buffer1[18]);
fputs(buffer2, in);
}
position = ftell(out);
line += 1;
get_line(buffer1, out);
}
...........................
...........................
} while (1);


void get_line(char *line, FILE *fp)
{
char ch;

position = ftell(fp);
ch = fgetc(fp);
while(ch != EOF && ch != 0xa)
{
*line++ = ch;
ch = fgetc(fp);
}
*line = 0; // add terminator
}

Please help me out. If I change position statement in get_line( ) function into "position = ftell(fp);", it does get a line of char. I guess something wrong with EOF.

Thanks

 
Change your declaration of ch to int. At EOF, fgetc returns an int of -1. Not sure what this converts to in a char and it also depends on whether your compiler treats char and unsigned char as the same thing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top