Can you put a text file into an array?

BlitzMax Forums/BlitzMax Beginners Area/Can you put a text file into an array?

Emmett(Posted 2005) [#1]
Is it possible to load a text file into an array?
file=ReadFile("readfile.bmx")

If Not file RuntimeError "could not open file openfile.bmx"

Global n=0 count=0 stringarray$[]

While Not Eof(file)
count:+1
     stringarray[n] = ReadLine(file)
n:+1
Wend

CloseStream file

For n = 0 To count
	Print stringarray[n]
Next 

My feeble attempt at modifying the "readfile" sample did not work.


FlameDuck(Posted 2005) [#2]
Yes. But since you can access a string by index, why not just put all of it in a String?

Incidently, you need to keep slicing the Array larger. Or use a list.


PowerPC603(Posted 2005) [#3]
file=ReadFile("MyFile.txt")

If Not file RuntimeError "could not open file"

Global count=0, stringarray$[]

While Not Eof(file)
	' Increase the total number of lines that have been read
	count:+1
	' Resize the array to hold one more line from the file
	stringarray = stringarray[..count]
	' Store the line in the array at the last index
	stringarray[count-1] = ReadLine(file)
Wend

CloseStream file

For Local n = 0 Until count
	Print stringarray[n]
Next


This will do the trick.

I'm using almost the same code in VB (in VB6 syntax of course) for reading a textfile (subtitles for DivX movies) into an array, processing the array to find misspelled words and then save the array to a new file.
But in VB, I used a preset size for the array (10.000 lines).


Emmett(Posted 2005) [#4]
Thanks PowerPC603
It did indeed do the trick and thanks for the comments so I know what each piece of code does exactly.
I'm using this to DrawText on screen. works great.