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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use file

Status
Not open for further replies.

tzzdvd

Programmer
Aug 17, 2001
52
IT
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
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
 
How about
Code:
FILE* logger;
long where;
logger = fopen (NAME_FILE, "r+");
fseek (logger, 0, SEEK_END);
where = ftell (logger);
if (where == 0)
{
   fprintf (logger, "<LOG>\n");
}
else
{
   fseek (logger, 8, SEEK_END);
}
fprintf (logger, "<EV %s ts=%s />\n", pEvent, cOrario);
fprintf (logger, "</LOG>\n");
fclose (logger);
 
This seems to be a good idea, and it seems to work too.
Now, we never go to the 'if' section but always to the 'else' section because this routine is executed when the file is already created with bytes written on it.

The question is:
why in this piece of software everything seems good and in my software not?

Davide



 
If you get stuck, step back and have a think or even talk to someone about it. The person doesn't even have to understand what you're talking about. Sometimes, as you're talking through it you may suddenly realize what the problem is.

The difficult part is admitting that there is a problem and it is your fault. Once you've gotten over that 'guilt' thing, it is really easy to even talk to a teddy bear and get a solution. You may see some older programmers talking to themselves while analyzing a problem. The screen is the teddy bear.
 
Good advice xwb !

Hehe, where I work, there is always a dull murmur as we all sit there muttering away to ourselves :)



--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top