Page 1 of 1

Searching a file for a string

Posted: Tue Sep 23, 2003 1:00 am
by Afrow_UK
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

Re: Searching a file for a string

Posted: Tue Sep 23, 2003 1:27 am
by frankiebaby
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!"

Re: Searching a file for a string

Posted: Wed Sep 24, 2003 10:19 pm
by Afrow_UK
Thanks very much!

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

-Stu

Re: Searching a file for a string

Posted: Sun Sep 28, 2003 9:50 pm
by Afrow_UK
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

Re: Searching a file for a string

Posted: Tue Sep 30, 2003 1:55 am
by Afrow_UK
Help!!

-Stu  :)