tags:

views:

28

answers:

1

I save in a file some info about users (like number of times user passed the login page, last visited time, and so on).

I want to read this info from the file, and update it (add 1 to the counter, and change the last visited time).

My question is: can I do it without opening the file twice ?

I open the first time to read the contents, and then open it again to overwrite the contents with the updated ones.

Thanks !

+1  A: 

Yes, you do this only opening the file once as follows:

  • open the file (i.e., fopen('data.txt','w+') )
  • read the data (fread)
  • write the data (fwrite)
  • close the file (fclose)
Matt
But after I read the contents the position of the file pointer will be at the end of the file, so why fwrite will write the contents at the beginning of the file ?
Misha Moroshko
Then take a look at rewind() as well
Mark Baker