searching through a text file

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
qbasicfreak
Veteran
Posts: 90
Joined: Wed Oct 22, 2008 3:27 pm
Location: canada

searching through a text file

Post 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
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
User avatar
qbasicfreak
Veteran
Posts: 90
Joined: Wed Oct 22, 2008 3:27 pm
Location: canada

thanks

Post by qbasicfreak »

thanks Ted

(Hello was just an example)
User avatar
qbasicfreak
Veteran
Posts: 90
Joined: Wed Oct 22, 2008 3:27 pm
Location: canada

Post 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
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
User avatar
qbasicfreak
Veteran
Posts: 90
Joined: Wed Oct 22, 2008 3:27 pm
Location: canada

Post by qbasicfreak »

thanks :lol:
Post Reply