LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Search a text file line by line

I want to search through the attached log file, line by line (looped) and count the occurance of certain variables. 
 
Please advise
0 Kudos
Message 1 of 6
(4,362 Views)
No attachment is present in your message. Anyway, a standard procedure could be this one:
 
OpenFile to open the file ready for reading
loop until not end of file
    ReadLine scans the file one line at a time. It returns -1 on error and -2 on end of file
    Scan or sprintf to scan the line and detect the elements of interest
CloseFile to close the file at end of the process
 
 
This subject has been discussed several times in the past and you should find enough reference on this commands in the forums. In any case, if you can give us more details on your actual problems we can try to give you some more help.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 6
(4,351 Views)

Hello,

I think you forgot to attach the file Smiley Wink

You could use the function ReadLine to read lines from your file, and then use Findpattern to search for the variable you are looking for. This piece of code counts the occurrence of the word "var" in the file:

 num_variables = 0;
 nr_of_readbytes = 0;
 while ((num_bytes = ReadLine (filehandle, line_str, MAX_STRING_LEN)) >= 0){
  
  // Search for variable "var" in the line.
  if (num_bytes > 0){
   variable_position = -1;
   do{
    variable_position = FindPattern (line_str, variable_position+1, -1, "var", 1, 0);
    if (variable_position >= 0)
     num_variables++;
   } while (variable_position >= 0);
  }
 }
 CloseFile (filehandle);
 // Check if an error occurred while reading from the file
 if (num_bytes == -1 && (error = GetFmtIOError ()))
  MessagePopup ("Error while reading from file", GetFmtIOErrorString (error));

Message 3 of 6
(4,347 Views)


(..)
loop until not end of file
(...)


Obviously this is a mistake: either loop while not end of file or loop until end of file Smiley Wink



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 6
(4,346 Views)
Kramje01 -

A good solution to this problem and others like it is to use regular expressions. 

fgrep will do the trick, if you can find a version that will run on windows.  MKS offers one.

You could write a simple CVI program to use a filter from a command shell.  The regular expression instrument is in the programmer's toolbox in CVI.  There's some overhead in learning how to use regexpr's but if you're doing software for a living you should know how to use these.

The windows command shell utility FIND wil do it too - you can make a simple batch file.  FIND doesn't offer regular expressions but it will tell you how many lines contain a particular string, if you're one occurance per line this will do it.

Menchar
0 Kudos
Message 5 of 6
(4,302 Views)
Thank you all very much for the help.  I was getting hung up on how to increment to the next line, but your sample code came in very handy.  Thanks again!!!
0 Kudos
Message 6 of 6
(4,289 Views)