LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how many lines in a text file and working with string data

 This is probably a simple thing most of you can do in your sleepk, but I am still a newbie at LV.  I have a text file that I am try to find data within.  A sample file might look like:

 

Roses are red

Violets are blue

blah blah

other swill

17 items

 

 

I am after the "17 items" portion.  It is on the next to the last line (the last line is a blank line immediately after "17 items".  Sometimes it might be a single digit number like "4 items".  Sometimes the file will have more lines in it than others, so I do not know which line will have my target info on it.

 

I was looking for the "items" and trying to get the value of "17".  I can search and find "items" but from there I can't get a vi to do what I want.  Please do not write it for me.  I like to write.  I just need help with the vi palette.

 

How can I find out how many lines are in my text file?  They have a CR/LF at the end.

Is there a way to read the last line in the file?

Does LabView have Right / Left / Mid commands for looking at sections of strings? 

 

Actually I did find a vi for Mid and Left command: the String Subset Function.  And String Length will tell me how many characters long a string is.  So is there something like a Right?

 

Thanks,

Steve Feeser

 

0 Kudos
Message 1 of 11
(6,721 Views)

 Well, since you said "don't write it for me", let's try a hint.  You can right click on the "read from text file" and tell it to "read lines". It will return a 1d array that you can easily search, scan through, etc.

 

Also, try posting what you have done so far so we can tell you why it does NOT work. Sometimes that is the more important lesson...

0 Kudos
Message 2 of 11
(6,715 Views)

My initial thought was to do the read all lines (have the Read Text File read by lines) and then you just run the lines through a FOR loop that contains a Scan From String with a "%d Items" as the format string.  Stop the FOR loop when you do NOT get an error (set conditional to Run While True).



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
0 Kudos
Message 3 of 11
(6,691 Views)

Here is the vi I have so far.  I am looking at one line at a time searching for the Desired String.  I increment Numeric if no match to look at the next line (I don't think that will work right the way I have it).  How can I end it when it finds the matching string?

 

 

0 Kudos
Message 4 of 11
(6,641 Views)

ReadLogFile_3PR[1].png

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 5 of 11
(6,629 Views)

I kicked it around some more and came up with this.  It seems to work.  Any suggestions?

 

I still would like to get the number off the front of the text string; that is what I am after.  I can do that in TestStand but it would be nice to do it all in LV.

 

Thanks,

Steve

0 Kudos
Message 6 of 11
(6,615 Views)

Here is my final solution.  It has a limit of 200 lines to search for the desired string.  If it does not find it in 200 it stops.  I can't picture this file ever reaching 100 lines long, so 200 limit is safe.  It gives an output of the number in front of the deisred string.

 

Comments?

 

Thanks,

Steve

 

0 Kudos
Message 7 of 11
(6,597 Views)

@SteveFeeser wrote:

Here is my final solution.  It has a limit of 200 lines to search for the desired string.  If it does not find it in 200 it stops.  I can't picture this file ever reaching 100 lines long, so 200 limit is safe.  It gives an output of the number in front of the deisred string.

 

Comments?

 

Thanks,

Steve

 


 

Are you still going with the "Don't write it for me, help me"?

 

Here are my comments:  

1.  Every loop iteration you read that file again.  So, if you scan 200 lines, you read the file 200 times!  

2. You never close your file. You should get in the habit of paying attention to closing references.

3. You ignored two different people telling you to right click on your "read from text file" and tell it to "read lines".  Wire a -1 in to the count, and it will return an array, with each element a line. You can then search it inside of a for loop with a conditional stop. Like crossrulz suggested.

0 Kudos
Message 8 of 11
(6,579 Views)

After reading the help sections for the first two vi's used here:

Read from Text File Function

Pick Line Function

I don't think it works like that.  I am thinking it works like this (maybe I am wrong):

The first vi (Read from Text File Function) says it opens, reads all the characters (I assume into memory) and then closes the file.

The second vi (Pick Line Function) then goes thru line by line presenting strings to the Match Pattern Function vi.

 

The Read from Text File Function does close the file so that is not a concern.

 

The Read from TextFile Function does have an option to read lines but it does not read one line at a time, like I want to search it.  (I want to search line by line to find the number at the beginning of the line.)  That vi reads however many lines you specify, always starting at the beginning of the file.  So, I did not see how that would help me...

 

Now if it really does open the file each time I increment the Pick Line Function vi then I can see that reading them into an array would be faster.

 

Wouldn't it do the Read from Text File, close the file and then do the Pick Line Function?

 

Thanks,

Steve

 

 

 

 

0 Kudos
Message 9 of 11
(6,568 Views)

OOOhhhhh but the Read From Text File is inside my While loop.  So it is doing it every time.....

 

0 Kudos
Message 10 of 11
(6,562 Views)