Showing results for 
Search instead for 
Did you mean: 

When to open\close file

Go to solution

GPC wrote:


... 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.


0 Kudos
Message 11 of 22



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?

Best regards,
CLAD expired, using LV2011SP1 + LV2017 (+LV2019 sometimes) on Win7+cRIO
Kudos are welcome Smiley Wink

0 Kudos
Message 12 of 22
I do not have all the data from the file in memory and many files are read in sequence. In my case it makes everything much easier to read it again.
I will change my program to make sure the writing/reading do not happen at the same time, using only one reference that will be opened an closed just once.
About this idea of having 2 references would that work? What actually happens when I open or close the file? If I write something on it and stop the program before closing the file what could happen?
Thanks again for the help, 
0 Kudos
Message 13 of 22
Accepted by topic author GPC

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.


Message 14 of 22

Hang on, Hang on...

Look at this picture, please.


You only have the correct value of size(s) in the very first iteration. :smileywink:



0 Kudos
Message 15 of 22

TerraNova wrote:

Hang on, Hang on...

Look at this picture, please.


You only have the correct value of size(s) in the very first iteration. :smileywink:



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.


0 Kudos
Message 16 of 22

So how, dan_u, You suppose to make it, again?


Please, picture or vi, please.



0 Kudos
Message 17 of 22

In your read loop, just insert the Set File Position primitive to set the file cursor to the start of the file before reading the contents.


0 Kudos
Message 18 of 22

It won't work out anything.


It will crash the content of te file.txt because the bottom loop activity will "hear" this file position change.


So Ups... :smileywink:



0 Kudos
Message 19 of 22

Well, for me it works. The 2 refs don't interfere with each other. LV 8.5.


0 Kudos
Message 20 of 22