From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Read last 10 lines from file

Solved!
Go to solution

Hello!
I am new to using Labview. I need to read last 10 lines from an config file (notepad). To this file will new lines added, so last 10 lines will be different each time. I want to display these 10 last lines on front panel. 
Do you have maybe some ideas? I'm trying to do it the second day and I have no idea..

0 Kudos
Message 1 of 4
(2,559 Views)

Do you know the lengths of the lines?

 

You can start reading bytes (not lines) relative from the end of the file. If you know how many bytes a line is, you can simply read bytes starting from a nr of bytes relative from the end (Set File Position).

 

If you don't know how many bytes a line is, you can't be sure of the offset. So you can start reading some number of bytes relative from the end of the file, and check if you get the required lines. If not, do it again with a larger offset. The right strategy is probably to read a bit more than expected, so you won't have to read twice. You will get more than you want, but you can use string functions (spreadsheet string to array) to get the last 10 lines.

 

A completely different strategy is to read the entire file at the start of the program, and then keep track of the file size while running. Parsing at the start will give you the entire file, so you should be able to get the last 10 lines. Each iteration, if the size increased, read the new data, and see if there is a new line. If so, add it and remove the first line (Delete from array).

 

If this file is created by your own application, there are probably better ways to do this, avoiding file IO altogether.

0 Kudos
Message 2 of 4
(2,533 Views)

Thank you! I'll try with reading bytes from the end, but could you tell me how to read from end? Using Read from Text File I can't choose where I start.

0 Kudos
Message 3 of 4
(2,521 Views)
Solution
Accepted by topic author anna172

@anna172 wrote:

Thank you! I'll try with reading bytes from the end, but could you tell me how to read from end? Using Read from Text File I can't choose where I start.


You have to use the other File IO and File IO\Advanced File functions.

 

Open/Create/Replace File

Set File Position

Read From Text File

Close File

 

Read From Text File accepts a path (that's what you're doing now) but also a reference. Get the reference from Open/Create/Replace File. Now you can Set File Position. If you open the file, you need to close it.

0 Kudos
Message 4 of 4
(2,512 Views)