LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Access the last row of text file

So I have the following: 

 

1) A text file, which is I have attached as "CH1234.log", which is a delimited spreadsheet with the /t delimiter.
2) I would like to pull the latest information and display on the HMI. The text file updates every 1 minute.   

ChristianLiinHansen_0-1625648929731.png

 

 Question: What is the most simple approach? Right now I am just playing around with this setup, where I am getting all the rows and the first row. Unfortunately there is no such "last row". So basically, should I just read all the rows, store it into an subarray and hence get the latest row in that array and display?

ChristianLiinHansen_1-1625649097053.png

 

 

0 Kudos
Message 1 of 9
(4,831 Views)

Hi Christian,

 


@ChristianLiinHansen wrote:

1) A text file, which is I have attached as "CH1234.log", which is a delimited spreadsheet with the /t delimiter.
2) I would like to pull the latest information and display on the HMI. The text file updates every 1 minute.   


You can read the full file, and parse the whole content into an array and get the last row of that 2D array. Once a minute is not that big problem…

 

You can improve the VI like this:

  • Store the current length of the file in a shift register. When the file gets updated (hopefully by appending new data at the end) you can determine the new filelength and only read the new data by using SetFilePosition before ReadFile…
  • You may use ReadTextFile to read an array of lines/rows from your text file. Now you get a 1D array of "rows": index the last one. (You may also use the SetFilePosition approach to read only the latest line…)
Best regards,
GerdW


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

@GerdW wrote:

Hi Christian,

 

You can improve the VI like this:

  • Store the current length of the file in a shift register. When the file gets updated (hopefully by appending new data at the end) you can determine the new filelength and only read the new data by using SetFilePosition before ReadFile…
  • You may use ReadTextFile to read an array of lines/rows from your text file. Now you get a 1D array of "rows": index the last one. (You may also use the SetFilePosition approach to read only the latest line…)

Hi GerdW. Thanks for your answer, but how do I use Read Text File to read the last array?

ChristianLiinHansen_0-1625652076492.png

 

0 Kudos
Message 3 of 9
(4,791 Views)

Hi Christian,

 


@ChristianLiinHansen wrote:
how do I use Read Text File to read the last array?

You use Read(Text)File to read the last (newly appended) bytes/chars as a string.

You can use SpreadsheetStringToArray to convert a string into an array afterwards. (ReadDelimitedFile does the very same as you can see in its block diagram!)

Best regards,
GerdW


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

@GerdW wrote:

You use Read(Text)File to read the last (newly appended) bytes/chars as a string.

You can use SpreadsheetStringToArray to convert a string into an array afterwards. (ReadDelimitedFile does the very same as you can see in its block diagram!)


But...:)  How will the block diagram looks like, in order to to use Read Text File to read the last bytes/chars as a string? 

Because the idea of reading the whole thing with array size and subtract to read the 2nd last line, looks pretty messy right now:

ChristianLiinHansen_0-1625654684851.png

 

0 Kudos
Message 5 of 9
(4,770 Views)

So I managed to think about a way to access the last line.  

ChristianLiinHansen_0-1625657790528.png

 

0 Kudos
Message 6 of 9
(4,760 Views)

Hi Christian,

 


@ChristianLiinHansen wrote:
Because the idea of reading the whole thing with array size and subtract to read the 2nd last line, looks pretty messy right now:

Use MatrixSize instead auf ArraySize followed by two IndexArray nodes: you get #rows and #columns directly!

Now you want to read the "2nd last row", before you always wanted to read "the last row": that's a difference! Maybe you should learn to write good questions before starting to code? 😄

 


@ChristianLiinHansen wrote:
How will the block diagram looks like, in order to to use Read Text File to read the last bytes/chars as a string?

Simple:

  1. You open the file, get the new filesize.
  2. Subtract the old filesize to know how many new bytes to read.
  3. Set the file position to the old filesize from beginning.
  4. Read the number of bytes/chars as calculated in step 2.
  5. Close the file.
  6. Store the new filesize in a shift register for the next read operation.
  7. Use SpreadsheetStringToArray to convert the string into an array…
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 7 of 9
(4,748 Views)

Well, looking in the logfile, there will always be appended a CR + LF, so why should I then want the last line? 🙂 Anyway, thanks for the help 🙂 

0 Kudos
Message 8 of 9
(4,741 Views)

Your file contains lines with metadata about actions, so you need to adjust the algorithm to find the last line with suitable data. You already got some good advice, so I won't repeat all that. There is definitely no need to read and parse more than a few hundred bytes.

 

What is writing this log? (Another LabVIEW program? Some third party program?). You might need to deal with access rights, e.g. if you try to read wile the other program has it open for writing. Make sure to open it read-only if you only want to read, etc.

0 Kudos
Message 9 of 9
(4,701 Views)