Matthew Mitchell
Matthew Mitchell

Reputation: 5393

C: Synchronising two file pointers to the same file

I need two file pointers (FILE *) to operate alongside each other. One is to apply append operations and another is for reading and overwriting.

I need appends to the file from one pointer to be recognised by the other file pointer so that the other file pointer can both correctly read and overwrite this appended data.

To synchronise the data, it appears that using fflush() on the appending file pointer works (at least for reading it does), but is this the correct way to achieve what I want and is it portable?

Thank you.

Upvotes: 0

Views: 1894

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129524

You should be able to do that with one pointer (and thus not having to sync unnecessarily). Just use fseek(f, SEEK_END, 0); when you want to add at the end. Use "rb+" to make the file readable and writeable.

As long as you don't use multiple threads to access the file, this should work just fine.

Upvotes: 4

Related Questions