LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Issue in reading a binary file, with 'Flatten to String' data.

Solved!
Go to solution

I'm facing issue while reading a binary file (created using LabVIEW).

I've mentioned everything (issue and method to reproduce it) within the attached VI.

 

Same vi is attached in 2012 and 8.0 versions.

 

--

Regards


I am not allergic to Kudos, in fact I love Kudos.

 Make your LabVIEW experience more CONVENIENT.


Download All
0 Kudos
Message 1 of 10
(5,296 Views)

I checked your VI, unplugged that error wire from "unflatten from string" just to see the full error message. It says " Unflatten or byte stream read operation failed due to corrupt, unexpected, or truncated data". 

Does it have something to do with the data you are providing right over that file path constant? 


0 Kudos
Message 2 of 10
(5,274 Views)

You need to remove the count constant and add a string constant for the data type in the read case as shown below.

 

Issue in VI.png

0 Kudos
Message 3 of 10
(5,270 Views)
Solution
Accepted by topic author moderator1983

The reading of a string from a binary file stops at a NULL character (0x00).  When the first character is 0x00, you are just reading the one character.  I would suggest writing to a byte array since you are doing the inverting.  And then you can read as a byte array.


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 4 of 10
(5,268 Views)

In supplying your "write to binary file.vi" with a refnum instead of a file path, you are telling it to append to the beginning of the existing file instead of replacing it. That could well screw up your unflatten because the VI is looking for a 5-element cluster and you're giving it extra stuff.

 

To test your program, take out the "encrypt" until you have the read/write stuff correct. You're just making finding your problem harder.

 

Cameron

 

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
0 Kudos
Message 5 of 10
(5,255 Views)

@camerond wrote:

In supplying your "write to binary file.vi" with a refnum instead of a file path, you are telling it to append to the beginning of the existing file instead of replacing it. That could well screw up your unflatten because the VI is looking for a 5-element cluster and you're giving it extra stuff.


Look closer.  He is using "Create or Replace" for the file open.  This means the file will be totally erased if one was already there.  There will not be any extra stuff.


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 6 of 10
(5,250 Views)

@crossrulz wrote:

@camerond wrote:

In supplying your "write to binary file.vi" with a refnum instead of a file path, you are telling it to append to the beginning of the existing file instead of replacing it. That could well screw up your unflatten because the VI is looking for a 5-element cluster and you're giving it extra stuff.


Look closer.  He is using "Create or Replace" for the file open.  This means the file will be totally erased if one was already there.  There will not be any extra stuff.


You're right, I missed that. My bad.

 

Cameron

 

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
0 Kudos
Message 7 of 10
(5,246 Views)

 


@crossrulz wrote:

The reading of a string from a binary file stops at a NULL character (0x00).  When the first character is 0x00, you are just reading the one character.  I would suggest writing to a byte array since you are doing the inverting.  And then you can read as a byte array.



I am not allergic to Kudos, in fact I love Kudos.

 Make your LabVIEW experience more CONVENIENT.


0 Kudos
Message 8 of 10
(5,233 Views)

@moderator1983 wrote:

 


@crossrulz wrote:

The reading of a string from a binary file stops at a NULL character (0x00).  When the first character is 0x00, you are just reading the one character.  I would suggest writing to a byte array since you are doing the inverting.  And then you can read as a byte array.



After playing around a little more, I think I might have misinformed you a little.  If you explicitly tell it a string, it looks for the string length at the very beginning and reads that length of bytes as a string.  It appears that if you implicitly tell it to read a string (not wire the data type) it reads all of the bytes directly, including the length of string you wrote.

 

Regardless, my advice is the same.  You should just write and read using byte array.  It is less conversions if you are performing your "encryption".

 

EDIT:  Here's a snippet of the VI I was playing with to figure this out.


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 9 of 10
(5,226 Views)

Ha.  After some more playing around, I discovered all you needed to do was set the "Append Array/String Size" on the binary write to FALSE.  Or you could just explicitly tell the read to read a string.

 

But again, why convert back to a string when you don't have to.  Here's the solution I was proposing for everybody to see.


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 10 of 10
(5,214 Views)