Page 1 of 1
searching through a text file
Posted: Mon Nov 17, 2008 12:39 pm
by qbasicfreak
How can I search through a text file for a string and then retrieve the next line of code?
example
I am searching for hello
the text file looks like
hello, Hi how are you
bye, see you later
The program whould return hello because it is the next phrase after hello
thanks
Posted: Mon Nov 17, 2008 3:04 pm
by burger2227
HUH? I guess you want hello included.
Since the file is all text, you can use LINE INPUT #1, textline$ to get each line of text in a loop. Watch out for EOF!
Then you can check for "Hello" using start = INSTR( textline$, "Hello"). INSTR returns the first position of the word in the line. Then you can stop reading the file when it returns anything greater than 0. Zero designates a word was not found in that text line.
If you only want the text from the word to the end, just use that position in : MID$ (textline, start, LEN(textline$) - (start - 1))
Ted
thanks
Posted: Mon Nov 17, 2008 3:55 pm
by qbasicfreak
thanks Ted
(Hello was just an example)
Posted: Mon Nov 17, 2008 4:00 pm
by qbasicfreak
one more question
is is possible to search for a string
eg
start = INSTR( textline$, string$)
I'd try it for myself but my computer is down and I'm having to use a friends computer that runs linux. Thanks
Posted: Mon Nov 17, 2008 4:07 pm
by burger2227
YES, naturally you can substitute a variable.
INSTR also allows you to search from a starting point:
start% = INSTR(begin%, teststring$, word$)
You can omit it if you begin at the first letter of text.
This allows you to do multiple searches for more than one repetition in a text line. Just use begin% = start% + 1 in a loop to look for more instances of a word or phrase (assuming you already found one: start% > 0).
Tell Linus I said hi, lol
Ted
Posted: Tue Nov 18, 2008 6:40 am
by qbasicfreak
thanks