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
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