LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

what happens to a file reference at the end of reading a file?

Solved!
Go to solution

General File I/O question -

 

If i am reading from a binary file and the read operation reaches the end of this file, what happens to the file reference?  Does it still exist in memory at that point, or is it released once labview issues the End Of File error?

 

I've coded a playback "start" and "stop" functionality and the "Stop" button is the only method in place for closing the playback file reference.  Immediate curiosities are

                 a) what happens to the file reference once the read operation reaches the end of the file

                 b) Does it matter that I only ever close the file reference once the user clicks "stop"?  (i.e. is there a better practice out there)?

 

 

I'm running Labview 8.6 on Vista Business.

0 Kudos
Message 1 of 10
(4,567 Views)

Hi jamoore84,

you can use the "Not a Refnum" function to check if it's still valid. You can use this function in your stop event to decide if you have to close or not.

 

Mike

0 Kudos
Message 2 of 10
(4,560 Views)
Solution
Accepted by topic author jamoore84

The file reference is valid when reaching the end of the file. Every read operation will return an error but you can write at this location. The write will succeed since this is normal when appending data to a file.

 

Closing the reference when the user hit stop is OK. If you will implement a Replay button you just need to reposition the file pointer to the beginning of the file and read again. This avoids the need to close and open the file again.

Waldemar

Using 7.1.1, 8.5.1, 8.6.1, 2009 on XP and RT
Don't forget to give Kudos to good answers and/or questions
Message 3 of 10
(4,558 Views)

Mike and Waldemar,

 

Thanks for the quick responses.  I do use the "Not A Number/Path/Refnum Constant" to check to see if the reference exists before closing the file.

 

Regarding setting file positions - where is the best place to implement this function? Using the "Read Binary File.vi" shipping example, would the best place be between the "Get File Size" and "Read From Binary File" functions, or is any time after the "Open/Create/Replace File function OK?

 

One stumbling block I've encountered is that I am unable to playback more than one file per VI session.  I doubt that it's due to setting file positions, though.

 

Thanks for your help!

 

Jimmy

0 Kudos
Message 4 of 10
(4,548 Views)

Hi,

 

As you can see in attached screenshot, the file reference still exists after the read operation reaches the end of file (refnum is the same before and after the reading operation, whereas I have reached the end of file).

 

As far as I understand your application, the only problem in closing file reference only when the user clicks "stop" is that the user will NEED to click "stop" in order to close the file reference. I guess that you also want to close the file refernce when the read operation reaches the end of the file : so there are 2 ways to do that.

1. You can read the file until an error occurs (error code 4 : end of file encountered), clear this error, then close the file reference. (easier method)

2. You can determine the file size thanks to "Get File Size" function, so if your file data structure is simple you will be able to know how many data you have to read (for instance, if there are U32 in your file, you will read 4 bytes per reading operation, so the number of reading operation needed will be "file size/4")

 

Hope this help

0 Kudos
Message 5 of 10
(4,547 Views)

krsone,

 

I don't think my reading operation is overly "complicated", but perhaps a bit more involved than a simple linear read.  Your mention of the "Get Size of File" function brings up an additional question I wanted to ask: 

 

If there is no value specified at the count terminal of the Read From Binary file function, does this prompt labview to read in the entire file the first time the read function is encountered?

 

I'm using a Producer/Consumer Loop structure to collect and process data, with an additional State Machine loop to monitor for button clicks.  The file reading (playback) and processing operations both take place in the Consumer Loop, while the file opening and closing take place in the State machine loop.  I use indicators and local variables to route the file references to the proper places.

 

due to the nature of the data I've collected [a collection of composite images/frames], I want to read a saved file and display it "a frame at a time".  To bring this sidebar back on track, do I need to specify a count size to do this, given the read function resides in a while loop?

 

Sorry if I've hijacked my own thread - I can continue this in a separate post if that would be more appropriate.

 

-Jimmy

0 Kudos
Message 6 of 10
(4,536 Views)

If there is no value specified at the count terminal of the Read From Binary file function, you will read only 1 data at a time. For example, in a for loop, if your file position is initially set to 0 (0 byte from file start) :

- iteration 0 => you will read the first byte and file position pointer go to 1

- iteration 1 => you will read the second byte and file position pointer go to 2

- iteration 2 => you will read the third byte and file position pointer go to 3

- etc...

 

For the other question, you should post some code. Smiley Happy

Message 7 of 10
(4,526 Views)

krsone,

 

thanks for the information- I'm going to pull the plug on this thread (seeing as the original question was answered) and create a new thread to address my code-specific woes.

 

thanks to all for the replies.

0 Kudos
Message 8 of 10
(4,496 Views)

I'm using a Producer/Consumer Loop structure to collect and process data, with an additional State Machine loop to monitor for button clicks.  The file reading (playback) and processing operations both take place in the Consumer Loop, while the file opening and closing take place in the State machine loop.  I use indicators and local variables to route the file references to the proper places.


This is bad programming style.

 

Do all file processing in the Consumer loop. Let your state machine loop send a message to the Consumer loop when you need to open or close the file.

 

Waldemar

Using 7.1.1, 8.5.1, 8.6.1, 2009 on XP and RT
Don't forget to give Kudos to good answers and/or questions
0 Kudos
Message 9 of 10
(4,479 Views)

This is bad programming style.

Excellent - hopefully this means my program and coding style will improve.  I've created a new thread to deal with this issue, seeing as this discussion has taken us away from the original post topic.

 

The saga continues: http://forums.ni.com/ni/board/message?board.id=170&thread.id=393756

 

 

Message 10 of 10
(4,467 Views)