readint problem

BlitzMax Forums/BlitzMax Programming/readint problem

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

Aight... I finally mustered up the time to squeak this one out for you. This one is a bit advanced, as it uses banks and streams with arrays, BUT... It does read and write binary.

	Function ReadBinary:String[](path:String)
		
		Local i:TStream = ReadStream(path)
		Local bank:TBank = New TBank
		Local bankstream:TBankStream = CreateBankStream(bank)
		Local aryData:String[]
		Local intI:Int = -1
			
		CopyStream(i, bankstream, StreamSize(i))
		CloseStream(i)
		SeekStream(bankstream, 0)

		While Not Eof(bankstream)
			
			' Read in the binary
			intI :+ 1
			aryData = aryData[..intI + 1]
			aryData[intI] = ReadInt(bankstream)
			
		Wend

		CloseStream(bankstream)
		
		Return aryData
		
	End Function

	Function WriteBinary(data:String[], path:String)

		Local o:TStream = WriteStream(path)
		Local intLoop:Int = 0
		
		If Not o = Null Then
			' Save the data 1 value per line
			For intLoop = 0 To data.Length - 1
				WriteInt(o, Int(data[intLoop]))
			Next
			
			CloseStream(o)
		End If
		
	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 aryOut:String[] = Split("64 48 17 206 142 214"," ")
		Local aryIn:String[]
		Local strPath:String = "binarytest.dkm"
		Local intLoop:Int = 0
		
		Try
			Print "Creating binary file..."
			WriteBinary(aryOut, strPath)

			Print "Reading binary file created..."
			aryIn = ReadBinary(strPath)

			Print "Displaying file contents..."
			For intLoop = 0 To aryIn.Length - 1
				Print "Position " + intLoop + ": " + aryIn[intLoop]
			Next
			
		Catch ex:Object
			Print "Error occured: " + ex.ToString()
			
		End Try
	End Function
	
	Main()


Keep in mind there are OTHER keywords you'll use writing and reading binary: Check out the STREAMS part in BlitzMax help.

Hope this helps :)