Reading a Stream

BlitzMax Forums/BlitzMax Beginners Area/Reading a Stream

Eric(Posted 2006) [#1]
I have a Comma Seperated Text File

 "Field1","Field2","Field3"


I am going crazy trying to figure out how to parse this simple little file.

There are about 250 lines each containing about 25 fields.

Is there anyone who has good code to do this?

Any help would be greatly appreciated.

Regards,
Eric


Brendane(Posted 2006) [#2]
Here's an example of a brute force method.... it takes your input string (your file) and gives you each word between quotes until it returns 0 :-

SuperStrict

Function GetNextWord:Int( inString:String, pos:Int Var, outString:String Var )
	Local state:Int = 0 ' 0=not begun, 1=begun copying, 2 = complete
	
	outString = ""
	While (pos <= inString.length) And (state < 2)
		Select state
			Case 0
				If inString[pos] = Asc("~q")
					state :+ 1
				EndIf
			Case 1
				If inString[pos] = Asc("~q")
					state :+ 1
				Else
					outString :+ Chr(inString[pos])
				EndIf
		EndSelect
		
		pos :+ 1
	Wend
	
	Return (state = 2)
EndFunction



Local s:String = "~qfield1~q, ~qfield2~q, ~qfield3~q ~qetc.~q"
Local pos:Int = 0
Local outString:String

While GetNextWord( s, pos, outString )
	Print outString
Wend



Dreamora(Posted 2006) [#3]
there is a great split function in the code archivs as well that would give you back an array of strings from a single string seperated by the given seperator


Booticus(Posted 2006) [#4]
http://www.blitzmax.com/codearcs/codearcs.php?code=1650

By our banned friend Noel! I still use this one quite a bit. Its handy!