Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top