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.

Example Code

Find the Last Line & Number of Lines in a Text File

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

This example opens a text file and counts the number of lines, separates the lines into a String array, and then returns the last line in a separate indicator.


Description

When retrieving data from a file, we often need it in array form.  This VI takes a .txt file and converts it to an array of strings using "Spreadsheet String to Array".  As a bonus, this VI also returns both the number of lines in the .txt file and the last line in the .txt file.


Requirements: LabVIEW 2012 (or compatible). Hardware: none.


 Steps to Implement or Execute Code:

  1. Download and open file.
  2. Select the file to open in the "File Dialog" on the front panel.
  3. Run the example.

 Additional Information or References:

 

Find the Last Line n Number of Lines in a Text File FP.png

 

Find the Last Line n Number of Lines in a Text File sn.png

 


**This document has been updated to meet the current required format for the NI Code Exchange.**

 

 

Product Support Engineer
National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
crashingsux
Member
Member
on

does what requested, but it's not really smart to read the whole file. Better: Use set file and count backwards, until a line feed comes.

crossrulz
Knight of NI Knight of NI
Knight of NI
on

I don't understand why you are reading the file twice.  Why not just read the file by lines (like you second read, use -1 as the count), get the array size from that, index out the last line?


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
DUKMARIVAN
Member
Member
on

Hi Crashingsux,

can you please upload a quick example of what you mentioned, how to count backwards until you find an "x" character from the end of a text file.

thanks.

DUKMARIVAN
Member
Member
on

Hi LaRisa_M, could you please upload the example for LV 2011 V11, both files are telling me they're V12 when oppening them on LV 2011.

thanks.