LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading in data from a file and terminating at EOF

Hey all,

 

Im relatively new to labview so this may seem to be a simple question. My task is to read in a file with integers with "n" lines, until the end of file. I only need to read in the first 3 numbers of each line. Right now my code works for only one line of code, but when I try looping to iterate "n" lines, it gets stuck in an infinite loop. I am not sure how to make the while loop end at EOF. Any suggestions would be helpful. Thanks!

 

-Micah

0 Kudos
Message 1 of 6
(2,459 Views)

Hi Micah,

 

read the file line by line and stop on error:

check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 6
(2,454 Views)

This answer is a little too vague for my application so let me try to be more specific. The file I am reading from is being outputed from a prognastics reasoner algorithm. It will output 6 integer values per line, and for N amount of lines. I only need to read in the first 3 integers. The thing is, my program needs to grap the data whenever it is newly added, no matter how many lines are outputed, and only take the first 3 integers per line. Right now I am comparing the byte size of the file periodically, and if there is a difference the first 3 data points are grabbed using a for loop. My problem is, I am trying to put that for loop inside of a while loop (to iterate N lines until EOF) but it only seems to infinitally loop, even using the code from the previous comment. Oh, one more thing..after I read the data it still must be 1-D array. Thankyou!

 

-Micah

0 Kudos
Message 3 of 6
(2,449 Views)

Hi Micah,

 

- posting your code will help us to understand your problems much better

- when reading the file in a FOR loop you will also get an "EOF" error, you can still check the error cluster. I don't understand why you get stuck in an infinite loop when you check for errors...

- reading only the new lines is easy when you use the SetFilePosition function. You already know the file size before the new lines are added...

- my problem is: why do you add another FOR loop inside the while loop? That's not clear for me...

 

Post your VI!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 6
(2,439 Views)

Just read the whole file and toss out what you don't need. Keep it really simple. and don't intentionally cause errors-- downsteam code might miss-understand.

1b.PNG

 

 


"Should be" isn't "Is" -Jay
0 Kudos
Message 5 of 6
(2,432 Views)

I'm with Jeff.  By far the easiest way is to just read the whole file and use the Spreadsheet String to Array function to get all of the data into a numeric array.  You can then just use Index Array or Array Subset to get the values you actually care about.


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 6
(2,424 Views)