LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

File IO

Hi guys, 

I need your opinion on an File I/O operation that I need to do. ( Just to clarify - my code is working without any issue but seeking your opinion to see if there are better ways of doing this)

Here is what I need to do. 

I have a hex file and I need to append a new line to a line that starts with ":09000000". This line could be anywhere in the hex file. So, what I did was, I loop through the lines and till I find ":09000000" and breakout of the loop, offset the file pointer to the beginning of that line by providing the amount of offset bytes.

Then write to the file (based on the bytes). Code is below. 

Your feedback would be much appreciatedCapture.JPG

Thanks. 

 

 

 

0 Kudos
Message 1 of 7
(2,904 Views)

Hi guys, 

I need your opinion on an File I/O operation that I need to do. ( Just to clarify - my code is working without any issue but seeking your opinion to see if there are better ways of doing this)

Here is what I need to do. 

I have a hex file and I need to append a new line to a line that starts with ":09000000". This line could be anywhere in the hex file. So, what I did was, I loop through the lines and till I find ":09000000" and breakout of the loop, offset the file pointer to the beginning of that line by providing the amount of offset bytes.

Then write to the file (based on the bytes). Code is below. 

Your feedback would be much appreciatedCapture.JPG

Thanks. 

 

 

 

0 Kudos
Message 2 of 7
(2,892 Views)

The Get File Position doesn't need to be in the loop. Moving it to the right, outside the loop makes more sense. I doubt it will effect performance much, but it would be clearer to read by others or future you.

 

You might need a better fall back mechanism. What happens if the string is not found?

 

Oh, and I would straighten those wires. But that's just me. (I know it's not just me, but suit yourself.)

Message 3 of 7
(2,880 Views)

I'm conflicted.  On the one hand, I usually yell and scream "Attach your code, not a picture!", as that lets us "play with the code", "fix" it (keeping the wires running straight, doing a "cleanup" of code -- you might try the Broom item on the Menu bar), and even test out other algorithms.  On the other hand, if your code has sub-VIs, you'd need to attach them as well (compressing a Project Folder will usually do the trick), and if you are running a version of LabVIEW we don't have (I, for example, can't get LabVIEW 2017 to install without killing all of my LabVIEW development, and sometimes killing my entire Windows installation), we can't even open it.  Oh, well.

 

So here's just an observation for you to consider.  It appears that when you find the line that starts ":090000000", you back up 29 bytes (why 29?), append a new line, and terminate the file, discarding what was there.  The following seems to me to be logically simpler (though it might not be as fast, and might have "memory" problems, depending on the size of your file):

  • Open the file, read all of the lines into an Array of lines (you do this by specifying a Count of -1), then close the file.  No loop needed.
  • Search the Array using a For Loop with a Conditional Stop to stop the loop when you find a match.  Also use the output of the comparison to build the Output String (if there is no match, you output the String, which means you output all but the target string).  This automatically handles the case where the Target String is missing.
  • Append the new String.
  • Output the new Array of Strings (which, again, you can do with a single Write).

Here's one way to do the search:Strings up to Target.png

Bob Schor

Message 4 of 7
(2,860 Views)

Point taken weibe@CRAYA ! Thanks for raising it.

0 Kudos
Message 5 of 7
(2,849 Views)

Thanks Bob for your input. Can't say your solution didn't cross my mind. The reason why I decided against it, is because I was little worried about data corruption when writing back and forth to a file(large amount of data). The hex file goes to a microcontroller straight away after this file operation, therefore couldn't take a risk. Anyway, thanks for sharing your thought and I'll remember to attach the vi next time.  

0 Kudos
Message 6 of 7
(2,845 Views)

> On the one hand, I usually yell and scream "Attach your code, not a picture!"

 

I don't mind images, as long as they are embedded in the mail. An attached image is the worst of both worlds. If I need to download it, it might as well be a VI...

 

On a tablet (while traveling or while watching TV), an image is convenient... 

0 Kudos
Message 7 of 7
(2,815 Views)