LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading a specific text in a larg file

Hi Guys,
 
Is there a way of reading some text in a larg file? For example, I want read the second and third words in the second lind in the file, Is it possible to do that?
 
Many thanks
 
Mike
0 Kudos
Message 1 of 5
(3,498 Views)
Hi Mike,

when using 'Read from Text file' (LV8+) you can set the number of lines to read. So just set it to 2 or 3 and you get your lines to analyze...

When the number of characters per line is fixed (or approximatly known) you can also read just the number of characters and do the line splitting (if needed) on your own!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 5
(3,490 Views)
Thanks GerdW,
 
But the "Read from Text File" has mainly 2 input. one of them is for the file name and the other is for count. How can i specify the line to read and from where i start to read?
Actually what I need is to read specific lines no matter how many charecters in that lines, because actually I don't know the length of the lines.
 
cheers
 
Mike

Message Edited by q123456789 on 02-01-2007 08:15 PM

0 Kudos
Message 3 of 5
(3,475 Views)

Instead of using 'Read From Text File' put the 'Read From Spreadsheet File' on your block diagram.  Now, open it's block diagram.  In there you will find the old 'Read Lines From File' vi.  Drag it into your block diagram and delete the 'Read from Spreadsheet File' vi.  This is, IMHO, much more useful for reading text files as one of its outputs the position in the file where you stop reading (we will use this later).  I do not know why NI chose to take this output out of 'Read From Text File', I think they made a big mistake.

Now, you want to read the second and third words in the second line.  Here's how:  (Follow along in the attached image)

Wire the path to your file to 'Read Lines From File' and a constant 1 to 'number of rows'.  Leave other inputs at default.

Drop another instance of 'Read Lines From File' to the right of the first one. 

Wire the 'new file path' output of the first to 'file path' input of the second. Again wire a constant 1 to 'number of lines' to the second.

Wire 'mark after read' output of the first to 'start of read offset' of the second.  You now have the second line of the file.

Wire it to 'Match Pattern' with a constant space wired to 'regular expression'.  The 'after substring' will be the line from the second word on.

Wire this into 'Match Pattern', again with a space.  The 'before substring' output is the second word and the 'after substring' is the line from the third word on.

Wire this into 'Match Pattern', again with a space.  The 'before substring' output is the third word.

It's a little 'brute force', but it will work.  With a little extra work with loops and shift registers you could pull out any word in any line.

 

 

Message 4 of 5
(3,462 Views)
Hi Mike,

you didn't tell you need arbitrary inputs... Most often someone only wants to read the header of a file. And reading the second line from a text file is much more easier than to read the (lets say) 12345th line with unknown line size!

Roy: very nice answer. I also miss the old file handling vi's 😞
Best regards,
GerdW


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