readint problem

BlitzMax Forums/BlitzMax Programming/readint problem

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.

Sanctus:

Here's the code to read text. I added a few things like streams, and arrays, but it should be very easy to follow.

	Function Read:String[](file:String) 
		' Reads a TEXT file then returns a string array
		Local i:TStream = ReadFile(file)
		Local rawdata:String
		Local data:String[]
		
		If Not i = Null Then
			If Not Eof(i)
				rawdata = ReadLine(i)
				data = Split(rawdata , " ")
			End If
			
			CloseFile(i)
			
		End If
		
		Return data
		
	End Function


	Function Split:String[](source:String, delimiter:String)
		' Splits the data apart based on the delimiter specified
		Local i:Int
		Local array:String[1]
		Local value:String
		Local arrI:Int = -1
		Local temp:String = source
		
		Repeat
			i = Instr(temp , delimiter)
			arrI :+ 1
			array = array[..arrI + 1]
			
			If i > 0 Then
				' In string
				array[arrI] = Mid(temp , 1 , i - 1)
				
				temp = Mid(temp, i + 1, Len(temp))
			Else
				' Equal string
				array[arrI] = temp
			End If
						
		Until i = 0
				
		Return array
		
	End Function
	
	
	Function Main()
		Local intLoop:Int = 0
		Local aryData:String[]
		
		Try
			Print "Reading from file..."
			
			aryData = Read("test.txt")
			
			Print "Data found, reporting..."
			
			For intLoop = 0 To aryData.Length - 1
				Print "Position " + intLoop + ": " + aryData[intLoop]
			Next 
			
		Catch ex:Object
			Print "Error occured: " + ex.ToString()
			
		End Try
	End Function
	
	Main()