LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

get number of lines of a text file

Hello,

I need to know the number of lines a text file has.

I know that I can read the whole file line by line and count but I can't use that method because this takes about 20 seconds (I have to read very large files). Is there another option to get the number of lines of a file?

Thanks a lot!
Rico
0 Kudos
Message 1 of 9
(10,895 Views)
Hai Rico,
 
Hope this helps.
 
Thanks,
Mathan.
Download All
Message 2 of 9
(10,893 Views)
Hi Mathan,

Thanks for your reply!

Unfortunatley, all of your methods do not work, they all need to read the whole file first to get the number of lines and that's way to slow if you have big files.

Cheers,
Rico
0 Kudos
Message 3 of 9
(10,887 Views)

Hai Rico,

Search the forum by "find number of lines in a text file". You will get lot of informations from LabVIEW Champions.

Thanks,

Mathan

Message 4 of 9
(10,881 Views)
Hi Osiris,

the only option is: when your lines have all the same length you can simply divide filelength by linelength to get the number of lines...

If the lines can vary in length you have to count them - and load the whole file to do so...
Best regards,
GerdW


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

Hi Osiris,

here is a link to some solutions from altenbach, as mathan mentioned.
http://forums.ni.com/ni/board/message?board.id=170&message.id=208085&query.id=43949#M208085

Mike

0 Kudos
Message 6 of 9
(10,862 Views)

Another tip which will make things much faster - you need to optimize the amount of data you pull from the disk at once.  On Windows systems, this is about 65,000 bytes.  You should be able to achieve disk-limited performance.

One caveat - the end-of-line "character" can be CR, LF, CR/LF, or LF/CR depending on the operating system and system settings.  Practically, searching for either CR or LF, once you know, will do the trick.  Windows is usually CR/LF, so either will work.

0 Kudos
Message 7 of 9
(10,825 Views)
You can use the End Of Line Constant (String palette), and it will return the correct EOL based on the OS.
0 Kudos
Message 8 of 9
(10,810 Views)


Osiris81 wrote:
Unfortunatley, all of your methods do not work, they all need to read the whole file first to get the number of lines and that's way to slow if you have big files.

OK, define "big files"!
 
If reading line by line takes 20 seconds, reading the entire file as a plain string and processing as in the link to my old post should be very fast. You need to limit yourself to as few IO steps as possible. Can you show us your code?
 
Remember that operating in memory is very fast, while file IO is very slow. If the file is truly gigantic, you can read it in big sections as DFGray suggested.

In any case, you should really re-think your approach. Huge ASCII files are expensive in general because of all the formatting and scanning operations to get to the data. For large datasets you should use binary files, preferably with fixed record size.
Message 9 of 9
(10,792 Views)