Avoid Data Corruption When Writing Binary Files Using LabVIEW

Updated Aug 30, 2023

Environment

Software

  • LabVIEW

This article goes through some general recommendations when using the Write to Binary File Function in LabVIEW to avoid data corruption.

Take a look at the following recommendations if you are experiencing data loss when writing to binary files due to corrupted data:
  • If the PC shutting down partway through one of those writes is a big concern, you should be adding some header data to the file so that the program can recognize when writing is incomplete, or perhaps write a separate "journal" file alongside the data file that has this meta-data. Then, part of the application can verify the contents of the binary file and ignore the unfinished bits.
  • A handle should not still be held open in Windows 10 even after the app closes the file, to avoid this it is recommended to use a direct path instead of a reference. But even if you do an "atomic" operation via LabVIEW (or close the file), there's no guarantee the data will make it completely to disk in the case of a power loss because of caching effects and the architecture of modern SSDs. The OS may still be buffering it, and the SSD may still be buffering it in a DRAM cache (a situation where the OS may think it is written).
  • Saving two copies is only really helpful for saving to different media, like a local disk and a USB drive, or local and network. If the system powers off while saving the first file, the second file provides no utility. If you want more of the data to be available in case it is shut off you can turn off buffering and write the data to the file in smaller chunks instead of the whole array at once. 
  • Another recommendation is to do more manual reading back of the data and just manually read values back out until you do not get the full 8 bytes for a double and build your own array for example.
  • The OS is typically not designed to protect data being written/read at power failure. Even if you do not suffer any data loss for a specific power failure event, you should not rely on that will still happen in further events. To prevent this kind of issue, we recommend you to backup your systems with UPS.