LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Does closing a text file flush it?

We're using LV 2018, 32 bit.

We're writing to a text file and want to avoid losing data if the system crashes, hence we need to either flush or close the file relatively frequently, to ensure that any data that LabVIEW thinks has been written to the file does indeed get written and not left hanging around in a Windows buffer right before a crash.

If you want to explicitly flush a file, you can use the flush file function. But does anyone know if the Close File function also forces a flush? E.g. when you close a file, are you guaranteed that the Windows buffers will be flushed and the data will be written right then?

-------------------------------------------------------------
David Thomson Original Code Consulting
www.originalcode.com
National Instruments Alliance Program Member
Certified LabVIEW Architect
Certified Embedded Systems Developer
-------------------------------------------------------------
There are 10 kinds of people: those who understand binary, and those who don't.
0 Kudos
Message 1 of 4
(108 Views)

@Dave_Thomson wrote:

We're using LV 2018, 32 bit.

We're writing to a text file and want to avoid losing data if the system crashes, hence we need to either flush or close the file relatively frequently, to ensure that any data that LabVIEW thinks has been written to the file does indeed get written and not left hanging around in a Windows buffer right before a crash.

If you want to explicitly flush a file, you can use the flush file function. But does anyone know if the Close File function also forces a flush? E.g. when you close a file, are you guaranteed that the Windows buffers will be flushed and the data will be written right then?


I think flushing a file forces a disk write, while closing a file just closes it and leaves it up to the operating system to decide when to write it to disk.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 2 of 4
(101 Views)

I was going to say "Write a LabVIEW program to test your assumption", but then I realized that the problem was embedded in the phrase "right before a crash", and how do you get LabVIEW to "simulate a crash"?  [I suppose you could use the not-to-be-used LabVIEW "Abort" function ...

 

If you are writing your text file "intermittently", say every minute or minutes or so, you can save yourself the worry by doing the following:

  1. Open (for Read/Write).
  2. Go to EOF.
  3. Write new stuff.
  4. Close the file.

The file will be "vulnerable to a crash" only during the time you are updating it, and if a crash does occur during this (brief, I hope) time, you'll probably have the last-but-one version of the file sitting intact on the disk.  I'm pretty sure I've used this technique in the past ...

 

Bob Schor

Message 3 of 4
(65 Views)

My guess is that it depends if windows write caching is enabled (default?) or disabled.

 

I would assume that if write caching is enabled, windows will complete the write independent of LabVIEW so you should be OK unless Windows also crashes hard... 😄

0 Kudos
Message 4 of 4
(58 Views)