The faster road to a String from a file

BlitzMax Forums/BlitzMax Beginners Area/The faster road to a String from a file

byo(Posted 2009) [#1]
Hi, guys.

I'm using this code to get all the lines from a file:

Local file:TStream = ReadStream("test.txt")
Local str:String

While Not Eof(file)
	str = str + ReadLine(file) + Chr(10) + Chr(13)
Wend
CloseStream(file)


I realize that this is not fast, maybe a char by char copy would be faster.

How would you guys copy the entire content from a file to a string?

Thanks in advance.


grable(Posted 2009) [#2]
For the most part, you wont notice any difference. unless the file is huge of course..
I would do something like this in any case:
Local file:TStream = ReadStream("test.txt")
Local str:String = file.ReadString( file.Size())



Htbaa(Posted 2009) [#3]
Or this:

Local str:String = LoadText("test.txt")



xlsior(Posted 2009) [#4]
It may be faster to use LoadBank first to cram the entire file contents straight in a memory bank, and read it from there rather than incrementally from a file -- lots of small reads from disk are slower than doing bigger blocks all at once.