... the reading sets the cursor to the beginning of the file and then it reads it all ...
There you would have a race condition. If the write process just happens between setting the file position and reading the file you'll have the new data at the beginning.
Opening and closing the file might cause other problems if you do this at 2 places. Will you open it with r/w access in both places? Then you will get an error the second time you try to do so. This would need to be handled (try again after some time).
You could try opening 2 file refs (one r/w and one r only) for the 2 loops. I don't know exactly how this will work, but the read loop might not see all data unless you flush the file after writing.
after detecting the next race condition:
Why do you write all data to a file and then read the whole data back again? Why not hold the data in memory?
If you open the file 2 times only 1 instance can have r/w access. The other just has read access, so you can't mess up.
Stopping the program before closing the file is bad programming, but shouldn't lead to data loss, since the data is either in the buffer to be written to file or already written to file.
The idea was something like this:
- in your loop that writes data, open the file once and keep it open, write to file continuously (probably using Flush File to force the OS to write the data), close the file when the loop terminates
- in your master loop only open the file when you need to read the data. Open the file in read-only mode, read the data, close the file.
You could also create a SubVI with different methods (action engine) which takes care of all the file access. One method would be to open the file. One method to write data to the file, one to read the file contents, one to close the file. Like this you have one central place handling all the file access.
Hang on, Hang on...
Look at this picture, please.
Yes, because you don't rewind the file position. If you keep the file open you need to set the file position to the start in order the read the whole file. Else you just read what has been added since the last read operation.