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: 

Count lines in text file

Folks:

What's the best way to count the number of lines in a text file?

thanks,
Kevin
0 Kudos
Message 1 of 15
(17,192 Views)
How about using match pattern.VI, If you know the last character of the line~
and, count the number of the iteration until offset past match become -1.
0 Kudos
Message 2 of 15
(17,186 Views)
Kevin,
I don't know of an existing VI for reading the lines of a text file, but you could make one. I attached a vi (LV-6) that should work. It converts a text file to a string array (using CR as line deliminators) and then checks the array for size - which is the line count. See if it works for you. I tested it on a few text files and it was accurate. Alter it as needed for deliminators, etc.
Good Luck with it, Doug
0 Kudos
Message 3 of 15
(17,187 Views)
If all you need to do is count the lines in the text file, then use the "Read Lines from File" VI with the number of lines set to 1. Do this in a while loop that quits when the "EOF?" output from the "Read Line from File" goes TRUE. If you feed the Index from the while loop out through a tunnel (not a shift register), the value will be the number of lines in the text file. You will have to use a shift register to feed the "Mark after read" back into the "Start of read offset" in the Read VI.

The index for the while loop starts at 0, so the you do not have to subtract 1 to find the exact number of lines.

Sounds easy enough, I think.

Rob
0 Kudos
Message 4 of 15
(17,186 Views)
You must first define a 'line'. If by line, you mean number of physical lines of text displayed in a window, then you have a problem. However, if you mean anything before a line end, carriage return, or similar, then Timy's answer is the easiest, and probably most efficient means. If your definition of line is the same as the File definition (I don't know what that definition is), then the first answer, using the EOF check, is the way to go.

Keep It Simple...
Message 5 of 15
(17,186 Views)
I've updated this example to use a the newer "Read from Text File.vi" and output an I32 instead of an I32 array.
 
0 Kudos
Message 6 of 15
(16,890 Views)
I'm wondering if this might be the simplest solution to this question.
Message 7 of 15
(16,877 Views)
Even simpler and faster is to just wire the Array Size function to the output of the Read From Text File.
Message 8 of 15
(16,875 Views)
That is simpler and better.  Thanks Dennis.
Message 9 of 15
(16,869 Views)
You can also skip the open/create, you'll still get a file dialog. I guess EOL conversion is also not needed.
 
So the most minumalistic code is as follows. 😉
 

 
Has anyone done any benchmarks? Somehow I have the feeling that temporarily creating that big array of strings might not be the most efficient compared to reading all as one string and counting linefeeds. Who knows...? How big are your files?

Message Edited by altenbach on 09-29-2006 04:03 PM

Message 10 of 15
(16,857 Views)