LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
Darin.K

Default Count for Read From Binary File Should be -1

Status: New
I never really understood why the default count value for Read From Binary File was 1.  95 times out of 100 I am reading the whole file (-1), the other times I am reading a fixed chunk size which is certainly not equal to 1 (and in this case I expect to wire a value).  The 'default' or natural behavior is really to read one byte/word/long at a time?  That is about as inefficient as it gets.
2 Comments
RandyP
Member

I believe the default behavior is to read one (the first) data element from the file. Maybe this example will help:

read_from_bin_file.png

As you can see, I wrote to strings to the binary file separately. I then read the file twice. The first time, I used the default behavior, and the first written element was returned. The second time, I read all of the data from the file, and got an array containing both of the values I had written.

 

Most of the time I use a binary file, I write a cluster to the file once, and then read that back later. the default behavior works well for me because I only performed one binary write operation.

 

Note also that the Binary read is polymorphic, in a weird kind of way. If anything is wire to the count input (even if a constant "1" is wired!), the data output beomes an array of whatever data type is specified. If nothing is wired to the count input, the data output is the same type and dimension of the data type.

 

This has been interesting since its forced me to really try to understand the binary file functions. Thanks!

 

Edit: Hopefully it wasn't confusing that I used strings ("Apples" and "Oranges") in my example. any other data type could be used as well.

 

Another Edit: I don't know what happens if two different data types are written to the file and then the whole file is read. I'm guessing that each element would be cast to the data type in whatever way the type cast function would do it.

Message Edited by RandyP on 12-16-2009 08:47 AM
Message Edited by RandyP on 12-16-2009 08:49 AM
-Randy
-=--=-=-=-=-=-=-
Nothing like a good dose of LabVIEW to cure what ails ya'.
RandyP
Member

Yet Another Edit: So I tried writing a string and then an I32 to a binary file. When I read each one individually (not wiring the count input), they each read back fine. However, if I wired -1 to the count input, only the string was returned in the array, and if I wired 2 to the count input, I got an End of File Encountered error. So it appears that it's pretty important to know the structure of a binary file you're trying to read.

-Randy
-=--=-=-=-=-=-=-
Nothing like a good dose of LabVIEW to cure what ails ya'.