03-03-2020 02:30 PM
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.
03-03-2020 02:32 PM
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.
03-03-2020 02:41 PM
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?
03-03-2020 02:48 PM
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.
03-03-2020 02:55 PM
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?
03-03-2020 02:57 PM
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?
03-03-2020 03:08 PM
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)
03-03-2020 03:16 PM
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.
03-03-2020 08:58 PM
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