LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Read and write to an arbitrary file

Solved!
Go to solution

All binary read/write examples start with writing first, and then reading the known data type.

I would like to do a very simple thing: read an arbitrary file as a bytestream, format-independent, and then save same to another file.

 

For example, I would like to read an .avi file and then save read bytes into another file such, that by giving this new file the .avi extention I would be able to open it with windows tools as a movie.

 

I know it should be easy, but straightforward read/write with binary or text read/write did not work...

 

Thank you for your help --

0 Kudos
Message 1 of 5
(2,681 Views)

Are you trying to copy the file?  Use "Copy" on the Advanced File Functions.

 

Bob Schor

0 Kudos
Message 2 of 5
(2,661 Views)
Solution
Accepted by topic author MichaelSolo

The super simple way is to just use the Copy function in the File IO->Advanced File Functions palette.

 

But if you want to do it the hard way, just use the Read From Binary File with a U8 for the data type (not an array) and -1 for the number of elements to read.  Then use a Write To Binary File with the data you just read.  Now for where you are likely having issues (this has bitten me many many times).  There is a boolean input on the Write To Binary File called "prepend array or string size".  Set that to FALSE.  If that input is TRUE (default), then there will be an I32 written to the file before your byte array that tells whoever is reading how large the array is.  You likely do not want that.


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
Message 3 of 5
(2,605 Views)

Thank you!

I need to read the file and then send in via a particular network loop, then receive it, compare with original and save the result. Therefore, "copy" would not work for me. Besides, sometimes you'd want to change just a few bytes of the file, keeping the rest intact, and save the result.

 

Thanks a lot for the info! I will try it and see how it works. I am surprized this rather simple procedure requires knowledge of so many nuances.. One would think the defaults would be set such that a simple binary string read with wiring the output to the input of another should do that.

 

Will let you know how it worked.

 

Regards --

0 Kudos
Message 4 of 5
(2,580 Views)

I just implemented your suggestion and it works fine!

 

Thank you so much for helping with this --

0 Kudos
Message 5 of 5
(2,556 Views)