Hi,
I'm trying to use files access and I have a question.
If I haven't the file I can create it end direclty add some chars.
Then I try to open it, append other chars and close it
And again in a while(1) cycle.
Here is my question:
The first time I open the file in order to append chars I receive an error because the software recognize the lenght of the file 0 byte. All the next time everything is ok as I wish.
Here is the code in the while(1) statement
In *1* I read every time the correct lenght of the file.
In *2* the first time I read 0 (insead of the real lenght); the next time *1* and *2* are the same.
Any suggestion?
Thank's
Davide
I'm trying to use files access and I have a question.
If I haven't the file I can create it end direclty add some chars.
Then I try to open it, append other chars and close it
And again in a while(1) cycle.
Here is my question:
The first time I open the file in order to append chars I receive an error because the software recognize the lenght of the file 0 byte. All the next time everything is ok as I wish.
Here is the code in the while(1) statement
Code:
if (stat(NAME_FILE, & info) < 0 )
{
printf("errno = %d\n", errno);
fflush(stdout);
}
else
{
printf("lenght = %d\n", info.st_size); // *1*
fflush(stdout);
}
if ( (iFD = open(NAME_FILE, O_RDWR, S_IRWXU) < 0) )
{
printf("Log Event Server: Error opening Log file '%s'\n", NAME_FILE);
fflush(stdout);
return -1;
}
if (fstat(iFD,&info))
{
printf("errno = %d\n", errno);
fflush(stdout);
}
else
{
printf("lenght = %d\n", info.st_size); // *2*
fflush(stdout);
}
offset = -8;
i = lseek(iFD, offset, SEEK_END);
printf("I ==== %d, errno = %d, iFD = %d\n", i, errno, iFD);
fflush(stdout);
write(iFD, "<EV ", 4);
write(iFD, pEvent, iLenght);
write(iFD, " ts='", 5);
write(iFD, cOrario, strlen(cOrario));
write(iFD, "'></EV>\n", 8);
write(iFD, "</LOG>\n", 8);
close(iFD);
sleep(1);
In *1* I read every time the correct lenght of the file.
In *2* the first time I read 0 (insead of the real lenght); the next time *1* and *2* are the same.
Any suggestion?
Thank's
Davide