std::fs::OpenOptions::append behavior

I dont quite understand this part of the OpenOptions documentation:

One maybe obvious note when using append-mode: make sure that all data that belongs together is written to the file in one operation. This can be done by concatenating strings before passing them to write(), or using a buffered writer (with a buffer of adequate size), and calling flush() when the message is complete.

I don't understand wtat it means, logically everything should have a predictable result

This is to avoid race conditions. Other processes may write (append to) the file simultaneously. So if you don't write everything in one go, then the other process might just happen to jump in between your two appending writes, corrupting the contents of your file.

thank you!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.