Code archives/File Utilities/ReadFileAsString$( ) Fast

This code has been declared by its author to be Public Domain code.

Download source code

ReadFileAsString$( ) Fast by Michael Reitzenstein2003
Faster version of Peter Scheutz's ReadFileAsString$( ).

Note however that if the code fails on the ReadString (computer turned off etc) it will corrupt the first four bytes of the file!
Function ReadFileAsString$( File$ )

	Local File_Handle = OpenFile( File$ )
	Local Return_String$
	Local Start_Chars$
	
	If Not File_Handle
		
		Return
		
	EndIf
	
	If FileSize( File$ ) < 4
		
		While Not Eof( File$ )
			
			Return_String$ = Return_String$ + Chr( ReadByte( File_Handle ) )
			
		Wend
		
		Return Return_String$
		
	EndIf
	
	Start_Chars$ = Chr( ReadByte( File_Handle ) ) + Chr( ReadByte( File_Handle ) ) + Chr( ReadByte( File_Handle ) ) + Chr( ReadByte( File_Handle ) )
	SeekFile File_Handle, 0
	WriteInt File_Handle, FileSize( File$ ) - 4 
	SeekFile File_Handle, 0

	Return_String$ = Start_Chars$ + ReadString( File_Handle )
		
	SeekFile File_Handle, 0

	For Count = 1 To 4
		
		WriteByte File_Handle, Asc( Mid( Start_Chars$, Count, 1 ) )
		
	Next			

	Return Return_String$
	
End Function

Comments

None.

Code Archives Forum