LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with binary file writing

Solved!
Go to solution

Hello,

 

I have a file with an array of clusters and would like to add an element to that array. Unfortunately, this time, the binary write function does not work but, doesn't give an error. An error appears only after I try to read the file and get the EOF error.

 

Cimpo_1-1752070219212.png

 

 

Cimpo_3-1752070301505.png

 

Cimpo_4-1752070644099.png

 

Cimpo_5-1752070676036.pngCimpo_6-1752070709552.png

 

 

 

Cimpo_7-1752070916646.png

Cimpo_8-1752070931232.png

 

 

 

My biggest problem is that the write binary block is not working and I do not know why. The task I want to do is save the array of cluster in a file which can than be accessed later in the same VI or another VI.

 

Please be patient with me as I am new to LabVIEW, Thank you.

 

0 Kudos
Message 1 of 7
(189 Views)

You should probably know by now that nobody here can diagnose pictures. Once you attach your VI, I will have look.

 

Most likely you need to set the file position before reading. The current file positions where you are reading is at the end of the last write, i.e. at the end of the file.

 

(Also, the correct way to append an element to an array is "built array", not insert into array")

0 Kudos
Message 2 of 7
(185 Views)

The write is working. But after the write, the file pointer is at the end of the file. When you try to do a read starting at the end of the file, you will get an End Of File error. After the write, you need so set the file pointer back to 0 using Set File Position (I think it is in the File I/O->Advanced palette). Then the read will read the entire file including what you just wrote.

 

Additionally, instead of doing a read, append, and write, you can use the Set File Position to set the pointer to the end of the file and then just write the new value. This will be a lot more efficient.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 7
(183 Views)

Hello, 

 

Thank you for the feedback, I tried doing it your way as it makes sense that the pointer would be at the end of the file but, when I put the pointer back at the start and read it, I still do not see the new value appended.

 

Cimpo_0-1752090986966.png

 

Cimpo_1-1752091014516.png

Cimpo_2-1752091040485.png

Cimpo_3-1752091060496.png

 

 

Thanks again for the reply.

 

 

0 Kudos
Message 4 of 7
(144 Views)

The index of your probe display is not at zero, so you cannot see the first element. The probe shows data. You did not wire the count, so you only get one by default. What would happen if you would wire a "-1" to count?

 

altenbach_0-1752092170399.png

 

 

Again, attach your VI instead of endless pictures!!!!

0 Kudos
Message 5 of 7
(124 Views)
Solution
Accepted by topic author Cimpo

In this example, you wrote a scalar cluster to the end of the file and read an array from the start of the file. The array you got is the first array you wrote to the file.

 

You have to know how LabVIEW flattens data when writing to disk in order to understand what is going on: Flattened Data

 

When you write an array to a binary file, the array size is written before the actual data. If you then read an array, you get back an array of the originally written size, even if there are more elements that follow (because you wrote another element afterwards).

 

This behaviour can be overriden by wiring false to the prepend array or string size? terminal on top of write binary file.

 

As altenbach wrote, you can choose how many elements to read, -1 means to read all.

Note that you cannot use an array for the type input if you wrote scalars or used prepend array or string size? = false.

 

write/read binary file with arraywrite/read binary file with array

Message 6 of 7
(109 Views)

Thank you for the help, it worked in the end. I misunderstood how the file held data and did not understand that you could simply append an element at the end of the file and LabVIEW will read the file as an array.

 

Here is the final solution I have.

 

Cimpo_0-1752159266649.png

 

Thank you to everyone that replied and have taught me new things!

0 Kudos
Message 7 of 7
(44 Views)