Tutorial 038 Search any text file for that word
The following program is adapted from the one in the previous Tutorial .
It can be used to search any chosen text file on your computer for the occurrence
of any particular word.
Here' some output :
Search for the word : and
Press <SPACE> to choose a text file that
you wish to search
Full Pathname of this text file :
C:\Lit\Virgil03.txt
Number of words examined = 1552
<and> has been found 49 times
Press <SPACE> to go again...or F to search
another file for the same word
Listing :
REM : Searches a text
file for a specific word
REM : Best used with pure prose text
REM : Richard Weston, 2nd July 2003
MODE 8
INPUT"Search for the word : "s_word$
REPEAT
sw=0
COLOUR1
PRINT'" Press <SPACE> to
choose a text file that you wish to search"
G=GET
OFF
:
DIM of% 75, ff% 18, fn% 255
!of% = 76
of%!4 = @hwnd%
of%!12 = ff%
of%!28 = fn%
of%!32 = 256
of%!52 = 6
$ff% = "Text Files"+CHR$0+"*.txt"+CHR$0+CHR$0
:
SYS "GetOpenFileName", of% TO
result%
IF result% filename$ = FNnulterm$(fn%)
COLOUR7
PRINT'" Full Pathname of this
text file :"
COLOUR2
PRINT'filename$
:
fnum=OPENIN filename$
IF fnum=0 THEN PRINT "No ";filename$;"
data": END
:
n=0
COLOUR7
REPEAT
finished=FALSE
word$=""
REPEAT
temp=BGET#fnum
:REM Read byte
PROCprocess
UNTIL finished
IF LEN(word$)>1
THEN
n+=1
PRINTTAB(1,8)"
Number of words examined = ";n
IF word$=s_word$
THEN
sw+=1
PRINT'TAB(5)"<";word$;"> has been found ";sw;" times"
ENDIF
ENDIF
UNTIL EOF#fnum
CLOSE#fnum
IF sw=0 THEN PRINT'TAB(5)"<";
s_word$;"> is not present in this text file"
PRINT'
COLOUR2
PRINT'" Press <SPACE> to
go again...or F to search another file for the same word"
G$=GET$
CLS
PRINT"You are searching for the
word : "s_word$
UNTIL G$=" "
RUN
END
:
DEF FNnulterm$(P%)
LOCAL A$
WHILE ?P% <> 0
A$ += CHR$?P%
P% += 1
ENDWHILE
= A$
:
DEF PROCprocess
IF temp>64 AND temp<91 THEN
word$+=CHR$(temp)
ENDIF
:
IF temp>96 AND temp<123 THEN
word$+=CHR$(temp)
ENDIF
:
IF temp=45 THEN word$+=CHR$(temp)
REM^ hyphen
IF temp=10 THEN finished=TRUE
IF temp>31 AND temp<65 THEN finished=TRUE
IF temp=45 THEN finished=FALSE
IF temp>90 AND temp<97 THEN finished=TRUE
IF temp>122 THEN finished=TRUE
ENDPROC
Next Tutorial
Richard Weston's Homepage