Low Level File Operations Handling on Linux and Windows c++ -


eventhough there similar questions here or here, have question different case.

by using c/c++, want write bytes file. file has data. simply, update content of file : open, write , close. however, if fails during write , if unable handle failure (for example, application crash, interrupt, electricity shutdown etc.), guaranteed in output file between list below? situation can happen, cannot?

  1. file may empty (deleted existing values , couldn't write new ones)
  2. file stays locked
  3. file may contain both old values , new values (i.e. first 5 lines new values, last 5 lines old values)
  4. file may contain old value.
  5. anything other don't expect?

if can give me os independent approaches, glad

thanks

write new data file same name, 'tmp' extension. flush , close tmp file. delete original file. rename tmp file original file name.

on startup, scan folder files. delete tmp files name part matches existing 'source' file, (ie. system interrupted during tmp file write). rename tmp file name part not match existing source file, (the tmp file written, original file deleted system interrupted before rename).

this system depends upon atomicity of deleting original file. if succeeds, new data, if fails, old data. should never bits of each.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -