How to read an entire file into a string variable?

Blitz3D Forums/Blitz3D Beginners Area/How to read an entire file into a string variable?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Try this:

Bank = LoadFileToBank("Filename.txt")
Result = FindTextInBank("some text",Bank)
If Result = 0
	Notify "String not found!"
Else
	Notify "Result found at offset " + Result
EndIf

Function LoadFileToBank(Filename$)
	FILE = ReadFile(Filename)
	BankID = CreateBank(FileSize(Filename))
	ReadBytes BankID,FILE,0,BankSize(BankID)
	CloseFile FILE
	Return BankID
End Function

Function FindTextInBank(Txt$,BankID,Start = 0)
	Ptr = Start
	Repeat
		T$ = Chr$(PeekByte(BankID,Ptr))
		If T$ = Left$(Txt$,1)
			For N = 1 To Len(Txt$)-1
				T$ = T$ + Chr$(PeekByte(BankID,Ptr+N))
				If T$ = Txt$ Then Exit
			Next	
		EndIf
		Ptr = Ptr + 1
	Until Ptr >= BankSize(BankID) Or T$ = Txt$
	If T$ = Txt$
		Return Ptr
	Else
		Return False
	EndIf
End Function
It works. Don't know how fast it is with big files though...

[EDIT] Just did a test on a 170k file - took ~4s to find a text string right near the end of the file.

I haven't really used banks before - there's probably a way of optimising this so that it works in a fraction of the time it currently takes.

[EDIT 2] Try it with debug off! Same test as above takes about 0.3s. :))