Search code examples
c#file-iodata-acquisition

Should I keep a file handler open between append writes?


I am working in a project involving data acquisition. One very important requisite is described like this:

  1. At the beginning of the recording, a file must be created, and its headers must be written;
  2. As soon as the data-acquisition starts, the application should keep saving collected data to file periodically (typically once per second);
  3. Writing consist on appending data blocks to the file, atomically if possible;
  4. Should any error occur (program error, power failure), the file must be kept valid untill the last write before the error.

So I plan to use some Thread to watch for data received and write this data do file, but I don't know which practice is best (code below is not real, just to get the feeling):

First option: Single open

using (var fs = new FileStream(filename, FileMode.CreateNew, FileAccess.Write))
    fs.Write(headers, 0, headers.Length);

using (var fs = new FileStream(filename, FileMode.Append, FileAccess.Write))
{
    while (acquisitionRunning)
    {
        Thread.Sleep(100);
        if (getNewData(out _someData;))
        {                        
            fs.Write(_someData, 0, _someData.Length);
        }
    }
}

Second option: multiple open:

using (var fs = new FileStream(filename, FileMode.CreateNew, FileAccess.Write))
    fs.Write(headers, 0, headers.Length);

while (acquisitionRunning)
{
    Thread.Sleep(100);
    if (getNewData(out _someData;))
    { 
        using (var fs = new FileStream(filename, FileMode.Append, FileAccess.Write))
        {                       
            fs.Write(_someData, 0, _someData.Length);
        }
    }
}

The application is supposed to work in a client machine, and file access by other processes should not be a concern. What I am most concerned about is:

  1. Do multiple open/close impact performance (mind that typical write interval is once per second);
  2. Which one is best to keep file integrity safe in the event of failure (including explicitly power failure)?
  3. Is any of this forms considered a particularly good or bad practice, or either can be used depending on specifics of the problem at hand?

Solution

  • A good way to preserve file content in the event of a power outage/etc, is to flush the filestream after each write. This will make sure the contents you just wrote to the stream get immediately written to disk.