Code archives/File Utilities/ReadFileAsString$()

This code has not been declared by its author to be Public Domain code, and therefore should not be assumed to be so.

Download source code

ReadFileAsString$() by Peter Scheutz
(Posted 24 years ago)
Read a file into a string, fast.
Function ReadFileAsString$(file$)

	Local tmp$=SystemProperty ("tempdir") + MilliSecs() + "bbrsrf.tmp"
	Local ret$
	Local fbank=CreateBank(	FileSize(file$))

	f=OpenFile (file$)
		If Not f FreeBank fbank : Return ""
	
		ReadBytes fbank,f,0,BankSize(fbank)  	
	CloseFile f
	
	f=WriteFile (tmp$)
		If Not f FreeBank fbank : Return ""
		WriteInt f,BankSize(fbank) 
		WriteBytes fbank,f,0,BankSize(fbank)
	CloseFile f

	f=OpenFile(tmp$)
		If Not f FreeBank fbank : Return ""
		ret$=ReadString(f)
	CloseFile f

	FreeBank fbank

	DeleteFile tmp$

	Return ret$

End Function