LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

open file

I'm trying a test VI to see how Open File VI works. I have one VI that
opens a file "test.txt" and keeps it open (constantly reading from the
file). I have another VI running simultaneously that opens the file and
writes to the file "test.txt". Why is this allowed? I thought it would
let the file only be openned one at a time.

What if two VI's were trying to write to the same file at the exact same
time? Do I need to implement some sort of check for this? I have
multiple VI's running that 'could' write to the same file at the same
time.

thanks,
stephen
0 Kudos
Message 1 of 4
(3,153 Views)
Use the deny mode input of the Open File node. The default is to allow
simultaneous reads and writes on files.

Jean-Pierre Drolet


"Stephen Gray" a écrit dans le message news:
3A71A3EF.36D39A04@swri.org...
> I'm trying a test VI to see how Open File VI works. I have one VI that
> opens a file "test.txt" and keeps it open (constantly reading from the
> file). I have another VI running simultaneously that opens the file and
> writes to the file "test.txt". Why is this allowed? I thought it would
> let the file only be openned one at a time.
>
> What if two VI's were trying to write to the same file at the exact same
> time? Do I need to implement some sort of check for this? I have
> multiple VI's running that 'could' write to the same f
ile at the same
> time.
>
> thanks,
> stephen
0 Kudos
Message 2 of 4
(3,153 Views)
there are several aspects of file i/o that you need to work with.
The first is file permissions.


open mode specifies whether this function opens the file for both reading
and writing data, for reading only, or for writing only. open mode defaults
to 0 if you do not wire it.

0 May both read and write to file.
1 May only read from file.
2 May only write to file. Does not truncate (remove all data from) the file.
Under any non-Macintosh platform, this mode acts like mode 0.
3 May only write to file. Truncates the file.

deny mode specifies the degree to which other users can operate on the file
simultaneously. deny mode defaults to 2 if you do not wire it.

0 Deny both read and write to the file by other users.
1 Permit read but deny write to the file by other users.
2 Permit both read and write to the file by other users.

Since the default permission is to permit both read and write, that would
explain why your test case works as it does.

Arbitrating the usage of the file can be accomplished a couple of ways.
If one process opens the file for exclusive read/write, other file opens
will fail and they will have to implement a retry mechanism.
If one process opens the file for mutual use, both processes will have to
use file locks with retries to access various parts of the file.

You will have to code the retry mechanism yourself.

Stu

Stephen Gray wrote in message
news:3A71A3EF.36D39A04@swri.org...
> I'm trying a test VI to see how Open File VI works. I have one VI that
> opens a file "test.txt" and keeps it open (constantly reading from the
> file). I have another VI running simultaneously that opens the file and
> writes to the file "test.txt". Why is this allowed? I thought it would
> let the file only be openned one at a time.
>
> What if two VI's were trying to write to the same file at the exact same
> time? Do I need to implement some sort of check for this? I have
> multiple VI's running that 'could' write to the same file at the same
> time.
>
> thanks,
> stephen
0 Kudos
Message 3 of 4
(3,153 Views)
> I'm trying a test VI to see how Open File VI works. I have one VI that
> opens a file "test.txt" and keeps it open (constantly reading from the
> file). I have another VI running simultaneously that opens the file and
> writes to the file "test.txt". Why is this allowed? I thought it would
> let the file only be openned one at a time.
>
> What if two VI's were trying to write to the same file at the exact same
> time? Do I need to implement some sort of check for this? I have
> multiple VI's running that 'could' write to the same file at the same
> time.

This is allowed for those rare situations where it is useful.
To disallow this, use the deny mode parameter when the file
is opened.

Greg McKaskle
0 Kudos
Message 4 of 4
(3,151 Views)