title: Transactional handling of text files on Windows
body: <p>I have multiple Windows programs (running on Windows 2000, XP and 7), which handle text files of different formats (csv, tsv, ini and xml). It is very important not to corrupt the content of these files during file IO. Every file should be safely accessible by multiple programs concurrently, and should be resistant to system crashes. <a href="https://stackoverflow.com/a/7297970/600135">This SO answer</a> suggests using an in-process database, so I'm considering to use the <a href="http://en.wikipedia.org/wiki/Microsoft_Jet_Database_Engine" rel="nofollow noreferrer">Microsoft Jet Database Engine</a>, which is able to <a href="http://msdn.microsoft.com/en-us/library/ms974559.aspx" rel="nofollow noreferrer">handle delimited text files</a> (csv, tsv), and <a href="http://msdn.microsoft.com/en-us/library/office/aa190107%28v=office.10%29.aspx" rel="nofollow noreferrer">supports transactions</a>. I used Jet before, but I don't know whether Jet transactions really tolerate unexpected crashes or shutdowns in the commit phase, and I don't know what to do with non-delimited text files (ini, xml). I don't think it's a good idea to try to implement fully ACIDic file IO by hand. </p>
  
  <p>What is the best way to implement transactional handling of text files on Windows? I have to be able to do this in both Delphi and C#.</p>
  
  <p>Thank you for your help in advance.</p>
  
  <h2>EDIT</h2>
  
  <p>Let's see an example based on @SirRufo's idea. Forget about concurrency for a second, and let's concentrate on crash tolerance. </p>
  
  <ol>
  <li><p>I read the contents of a file into a data structure in order to modify some fields. When I'm in the process of writing the modified data back into the file, the system can crash.     </p></li>
  <li><p>File corruption can be avoided if I never write the data back into the original file. This can be easily achieved by creating a new file, with a timestamp in the filename every time a modification is saved. But this is not enough: the original file will stay intact, but the newly written one may be corrupt. </p></li>
  <li><p>I can solve this by putting a "0" character after the timestamp, which would mean that the file hasn't been validated. I would end the writing process by a validation step: I would read the new file, compare its contents to the in-memory structure I'm trying to save, and if they are the same, then change the flag to "1". Each time the program has to read the file, it chooses the newest version by comparing the timestamps in the filename. Only the latest version must be kept, older versions can be deleted.</p></li>
  <li><p>Concurrency could be handled by waiting on a named mutex before reading or writing the file. When a program gains access to the file, it must start with checking the list of filenames. If it wants to read the file, it will read the newest version. On the other hand, writing can be started only if there is no version newer than the one read last time.</p></li>
  </ol>
  
  <p>This is a rough, oversimplified, and inefficient approach, but it shows what I'm thinking about. Writing files is unsafe, but maybe there are simple tricks like the one above which can help to avoid file corruption.</p>
  
  <h2>UPDATE</h2>
  
  <p>Open-source solutions, written in Java:</p>
  
  <ul>
  <li>Atomic File Transactions: <a href="http://www.onjava.com/pub/a/onjava/2001/11/07/atomic.html" rel="nofollow noreferrer">article-1</a>, <a href="http://www.onjava.com/pub/a/onjava/2002/02/06/atomic.html" rel="nofollow noreferrer">article-2</a>, <a href="http://www.onjava.com/onjava/2001/11/07/examples/atomic.zip" rel="nofollow noreferrer">source code</a></li>
  <li>Java Atomic File Transaction (JAFT): <a href="https://code.google.com/p/javaaft/" rel="nofollow noreferrer">project home</a></li>
  <li>XADisk: <a href="http://www.ibm.com/developerworks/opensource/library/os-xadisk/index.html" rel="nofollow noreferrer">tutorial</a>, <a href="https://xadisk.java.net/" rel="nofollow noreferrer">source code</a></li>
  <li>AtomicFile: <a href="http://developer.android.com/reference/android/util/AtomicFile.html" rel="nofollow noreferrer">description</a>, <a href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/util/AtomicFile.java" rel="nofollow noreferrer">source code</a></li>
  </ul>