readbyte()

BlitzPlus Forums/BlitzPlus Beginners Area/readbyte()

El Neil(Posted 2006) [#1]
ok heres the deal.

im writing a typing tutor game where people can paste their own text into a file from wherever to use for practice. The game reads the letters one by one from the file, but i need to skip all spaces, punctuation, carriage returns etc.

here is the code i have made so far:



Function readletter()


If Eof("YOUR WORDS HERE.txt") Then SeekFile("YOUR WORDS HERE.txt",1)

;basically, if eof, then loop the file


Repeat
e = ReadByte("YOUR WORDS HERE.txt")
Until e > 64 And e <91
; cycle through the file until the number returned from readbyte() is within ascii code for A-Z
Return e

End Function



now, when i run this it bombs out rather spectacularly. im guessing that the number returned by readbyte(0 isnt the ascii value. can someone please advise me as to what the integer returned by this function represents, and how i could convert it to the relevant ascii code?


thank you
Neil


GfK(Posted 2006) [#2]
I dont have Blitzplus so I wrote this version in Blitzmax. This scans for both upper and lower case letters - basically any character contained in valid:String.
SuperStrict

Local text:String = readtext("text.txt")
DebugLog text

Function ReadText:String(file:String)
	Local valid:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Local F:TStream = ReadFile(file)
	Local b:Byte
	Local result:String = ""
	If F
		While Not Eof(F)
			b = ReadByte(F)
			If Instr(valid,Chr$(b))
				result :+ Chr$(b)
			EndIf
		Wend
		CloseFile F
	EndIf
	Return result
End Function

Below is a version of the same code that SHOULD run in blitzplus - untested though.
Local txt$ = readtext("text.txt")
DebugLog txt

Function ReadText$(file$)
	Local valid$ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Local F% = ReadFile(file)
	Local b%
	Local result$ = ""
	If F
		While Not Eof(F)
			b = ReadByte(F)
			If Instr(valid,Chr$(b))
				result = result + Chr$(b)
			EndIf
		Wend
		CloseFile F
	EndIf
	Return result
End Function



El Neil(Posted 2006) [#3]
blimey. a bit more complex than i first thought then. thanx GfK. you're a legend.

neil