Searching a file for a string

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
Afrow_UK
Newbie
Posts:8
Joined:Sun Sep 21, 2003 9:36 pm
Searching a file for a string

Post by Afrow_UK » Tue Sep 23, 2003 1:00 am

I'm trying to write a function to search a file for a string, and once found, return with "yes" (for found).

Here's what I think is a finished function, but was wondering if you could check it through because I'm a QB newb.

FUNCTION SearchFile (FileName$, Name$)
NameLen = LEN(Name$)
SearchNum = 1
Found$ = "no"
OPEN FileName$ FOR INPUT AS #1
 DO UNTIL EOF(1)
  LINE INPUT #1, LineText$
  LineLen = LEN(LineText$)
  IF MID$(LineText$, SearchNum, NameLen) = Name$
   Found$ = "yes"
  ELSE
   IF SearchNum = LineLen THEN
    GOTO NextLine
   END IF
   SearchNum = SearchNum + 1
  END IF
NextLine:
 LOOP
CLOSE #1
Name$ = Found$
END FUNCTION

-Stu

User avatar
frankiebaby
Global Moderator
Posts:95
Joined:Tue Apr 30, 2002 1:38 am
Location:Pennsylvania
Contact:

Re: Searching a file for a string

Post by frankiebaby » Tue Sep 23, 2003 1:27 am

Thats a pretty good way to go about doing what u wanted, and it looks like it should work, but there are a few things u should do differently.

You are missing the point of a function. a function is used much like a variable. Instead of running a sub like this:

Dostuff (foo%)

U call it like this:

Foo% = Dostuff(foo2%)

this allows the result of the function to be used in statements and to assign variables and to be used as parameters for other functions (MID$ is a function)

so heres how u should do it: (mostly a copy and paste of what u did with a few edits)
typically, -1 =TRUE and 0 = FALSE, as that is how the language sees it, so IF -1 THEN THIS IS TRUE
IF 0 THEN this is false

Code: Select all

FUNCTION SearchFile% (FileName$, Search$)
  //now returns an integer

FOUND% = 0
 
 OPEN FileName$ FOR INPUT AS #1 
  DO UNTIL EOF(1) 
   LINE INPUT #1, LineText$ 
       IF INSTR$(LineText$, Search$) THEN FOUND = FOUND +1
         //INSTR$ returns a 0 if not found, else the number
        // of the position the text starts at. check the help
 LOOP 
 CLOSE #1 
SearchFile% = FOUND% // tells SearchFile what to return
END FUNCTION 
Ok so, heres why i made the changes:
1) Now function works properly as a function
2) Less Lines of Code, simpler design (less memory used)
3) Now, can be used to tell how many times that word appears at least once in a line. DOES NOT TELL HOW MANY TIMES IN THE FILE (for speed), can be modified to do so, however

ok, heres how to use that function:

IF SearchFile%("FileName.Ext", "Hello") THEN PRINT "FOUND!"

or:

PRINT "Hello was found"; SearchFile%("FileName.Ext", "Hello"); "Times!"

Afrow_UK
Newbie
Posts:8
Joined:Sun Sep 21, 2003 9:36 pm

Re: Searching a file for a string

Post by Afrow_UK » Wed Sep 24, 2003 10:19 pm

Thanks very much!

I'll be back :) (for more help likely)

-Stu

Afrow_UK
Newbie
Posts:8
Joined:Sun Sep 21, 2003 9:36 pm

Re: Searching a file for a string

Post by Afrow_UK » Sun Sep 28, 2003 9:50 pm

I had to modify the function to get it to work.

Code: Select all

FUNCTION SearchFile% (LogFile$, Search$)
FOUND% = 0
 OPEN LogFile$ FOR INPUT AS #1
  DO UNTIL EOF(1)
   LINE INPUT #1, LineText$
  IF INSTR(LineText$, Search$) THEN
   FOUND% = 1
   EXIT DO
  END IF
  LOOP
 CLOSE #1
SearchFile% = FOUND%
END FUNCTION
I am having a problem now compiling the program into an exe.
I'm using QB7.1, http://myweb.tiscali.co.uk/imker/PARSE.BAS
The compiler stops on the new search function call.

-Stu

Afrow_UK
Newbie
Posts:8
Joined:Sun Sep 21, 2003 9:36 pm

Re: Searching a file for a string

Post by Afrow_UK » Tue Sep 30, 2003 1:55 am

Help!!

-Stu  :)

Post Reply