Reputation: 3884
I am writing a program where I log all writes to a file and just before process termination I write all writes in place i.e from log to actual file. Now, in the log each record of type something like :
offset, length, data
After a write I need to calculate the offset so that I can lseek to that position when i finally write in the actual file so as to write the next write or for read.
Will current offset position+bytes written give me the file offset after a write.
Thanks
Upvotes: 0
Views: 2076
Reputation: 3954
There are several ways to achieve that:
First Way (In this case position will be updated automatically to how much you have read.)
Second Way (most portable)
Third Way
Fourth Way (less portable)
The difference between func() and func64() is that func64() uses correct 64-bit offset which eliminates problem with files more than 4Gb, while func() uses int type which could be 32 or 64 bits, depending on the architecture.
The fseeko() and ftello() functions are identical to fseek() and ftell() except that the offset argument of fseeko() and the return value of ftello() is of type off_t instead of long.
As a conclusion the best practice is to use fgetpos64()/fsetpos64() or ftello64()/fseeko64(). As much as possible try to avoid any using of ftell() and fseek().
Upvotes: 0
Reputation: 2340
Use fgetpos to get the file offset and fsetpos to set the file offset.
Upvotes: 1