From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, 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: 

New to LabVIEW. Need Help with Reading a Certain Word from a File

Problem: Write a VI that displays the number of times a word or characters specified by the user occurs in the file. The case-sensitivity of the search also needs to be determined by the user.

0 Kudos
Message 1 of 9
(2,044 Views)

This sounds like a homework problem.

 

Have you started?  Where are you stuck?

 

If you haven't started, then write down in words the steps you will need to do to solve the problem.

0 Kudos
Message 2 of 9
(2,041 Views)

In order to search through a file, you need to read the file into memory (i.e. a long string or a string array with words as elements, depending on the requirements) and search there. LabVIEW has all the tools for that. How far did you get? Do you have any specific questions?

 

You also need to define the problem better, because a "word" is different than "characters". Words are flanked by delimiters (spaces, newline, tab, etc.) while "characters" don't have that restriction. For example, if you search for "car" and the file contains the word "cargo", should it count or not?

Message 3 of 9
(2,027 Views)

This is a homework problem (I am a biomedical engineering student). I have started on it. I have gotten as far as getting the text file in there by using a file path and opening it. I know I need to read the text from the file, but I am not sure whether I need to scan the file or read it. The program needs to give the user the ability to choose what word or character is read from the file. What we want the program to do at the end is to display the number of times that certain word or character is written in the file. 

0 Kudos
Message 4 of 9
(2,020 Views)

So, I think the instructor made a mistake when he said words OR characters because later on, he specifies that the default search should be for the word "the". So I think that makes it a little easier. When I open a file (open file) in the block diagram, can I just wire that straight to a string, then make it search from there? I can't just search directly from the file? 

0 Kudos
Message 5 of 9
(2,015 Views)

It depends on how big the file is to know if you can handle it all at once.

 

There are functions in the string palette that will search for word patterns.  Have you found them?

 

Once you found the first one, what steps will you need to do to find the second, and any remaining?

0 Kudos
Message 6 of 9
(2,009 Views)

To search for words, you need to define all possible delimiters, then create an array of all words in lowercase.

(A good tool is Scan string for tokens, because it allows multiple alternative delimiters. See how far you get.

 

Then search the array for the lowercase word and count their number.

 

(Alternatively, you could use a map based solution (key=word, data=count), but let's not be too exotic yet. :D)

0 Kudos
Message 7 of 9
(1,997 Views)

Thank you both for your responses. I am going to take your advice and see what I can do with it. Hopefully, I can come back with something. 

0 Kudos
Message 8 of 9
(1,993 Views)

One of the nice things about LabVIEW is that it encourages (or should) "experimentation" and "exploration".  It also has a lot of at-hand Help (type ^H to turn Help "on", which lets you hover over a function and see a brief description of what it is called, what it does, and its), is fairly quick to program, and shows you "logic errors".  If you don't exactly know what something does, write a little bit of code and try it out.

 

So if you want to know how File I/O works, make a guess.  Wire some functions together, put an indicator at the end of, say, a Read Text (or a Read Binary), see what type of variable LabVIEW wants to put there.  You can run your program, see if there are Run-Time Errors, and examine the results.  If it seems "wrong", make some guesses about what to fix and how to fix it, and try again (it's really quick with LabVIEW, and you'll learn a lot from your mistakes).  Once you have some string data, look at the String functions and see if any of them might be helpful for the next part of your challenge.

 

If all else fails, pick a function that seems to be stopping you and read the Detailed Help, maybe look at an Example (if the Detailed Help points you to one).  Keep trying!

 

Bob Schor

Message 9 of 9
(1,938 Views)